Oracle行转列函数由wmsys.wm_concat到listagg的进化
测试表数据
select * from test01
ID NAME
10 CLARK
10 KING
10 MILLER
20 ADAMS
20 FORD
20 JONES
20 SCOTT
20 SMITH
30 ALLEN
30 BLAKE
30 JAMES
30 MARTIN
30 TURNER
30 WARD
1.wmsys.wm_concat用法(在10g下)
这个函数很常见,但Oracle官方文档未支持,11g后已过时
SELECT id, wmsys.wm_concat(NAME) agg_name
FROM test01 t
GROUP BY id;
查询结果:
ID AGG_NAME
10 CLARK,KING,MILLER
20 ADAMS,FORD,JONES,SCOTT,SMITH
30 ALLEN,BLAKE,JAMES,MARTIN,TURNER,WARD
2.listagg用法(在11g下)
SELECT id, listagg(NAME, ',') within GROUP(ORDER BY id) agg_name
FROM test01 t
GROUP BY id;
查询结果:
ID AGG_NAME
10 CLARK,KING,MILLER
20 ADAMS,FORD,JONES,SCOTT,SMITH
30 ALLEN,BLAKE,JAMES,MARTIN,TURNER,WARD