oracle行转列函数/通用行转列
Oracle 10g: wm_concat
Oracle 11g新增了函数:LISTAGG
简单例子:
SELECT a.id, wm_concat (a.remark) new_result FROM tb_name a group by a.id
可以配合over一起使用,具体复杂的用法可以到时再查
通用SQL:
在使用过程中发现有些oracle会存在查询不出数据的问题,这个无解,所以找了个纯sql的列转行
SELECT feeapportion_id, SUBSTR(MAX(SYS_CONNECT_BY_PATH(contract_no, ',')), 2) contract_no FROM (SELECT feeapportion_id, contract_no, rn, LEAD(rn) OVER(PARTITION BY feeapportion_id ORDER BY rn) rn1 FROM (SELECT feeapportion_id, contract_no, ROW_NUMBER() OVER(ORDER BY contract_no) rn FROM TH_SF_FEEAPPORTION_d)) START WITH rn1 IS NULL CONNECT BY rn1 = PRIOR rn GROUP BY feeapportion_id