问题描述:在开发中,需求:对一张含有多个 产品代码的产品表,一个产品代码对应2(多)个业务码,现在业务码122表示申购,124表示赎回,需将同一支产品的不同业务码对应的费率字段合并到同一条数据上显示。实际就是解决将数据列变数据行合并显示的问题。
| prd_code | busin_code | ... | fee_rate |
| 110002 | 124 | reebackfee | |
| 110002 | 122 | buyfee | |
| 110003 | 122 |
需要 实现的效果:
| prd_code | reebakfee | buyfee | busin_code |
| 110002 | ... | ... | 122,124 |
| 110003 | ... | ... | 122,124 |
| 110004 | ... | ... | ... |
解决方案:
方法一:将该产品表【tbproduct 】用prd_code 字段拼接一遍,新表数据【 tbproduct a,tbproduct b】的同一行中,既含有busin_code='122' 又含有busin_code ='124' 的数据
这样可实现多行数据同一行显示。
select a.prd_code,a.busin_code,a.rate,b.busin_code,b.rate from tbproduct a,tbproduct b where a.prd_code = b.prd_code and a.busin_code = '122' and b.busin_code = '124';
方法二:该方法适合ora 11以上的版本
select a.prd_code,listagg(a.rate,',') within group (order by a.busin_code) ind,listagg(a.busin_code,',') within group (order by a.busin_code) ind from tbproduct a group by a.prd_code
方法三:select t.prd_code,wm_concat(t.busin_code),wm_concat(t.rate) from tbproduct t group by t.prd_code;
【表名自行更换】
浙公网安备 33010602011771号