oracle 的列转行函数 listagg()

当你的表X中有A,B两列,数据如下

A  B

a  1

a  2

a  3

b  1

b  2

b  3

想让数据以 a|1|2|3 , b|1|2|3 格式显示可使用listagg()

1、使用listagg() + group by

select A,B,listagg(B,'|') within GROUP (order by A)  C from X group by A;

2、使用listagg() + over(partition by ?)

select A,B listagg(B,'|') within Group(order by A) over(partition by A)  C from X;

原文地址:https://www.cnblogs.com/wangkang0320/p/6707229.html