listagg
listagg (filedname,',') WITHIN GROUP (ORDER BY filedname)
行转列函数。
teledb=# create table person
teledb-# (
teledb(# deptno varchar2(10),
teledb(# ename varchar(20)
teledb(# );
CREATE TABLE
teledb=# insert into person values('20','aaa');
INSERT 0 1
teledb=#
teledb=# insert into person values('20','bbb');
INSERT 0 1
teledb=#
teledb=# insert into person values('20','ccc');
INSERT 0 1
teledb=#
teledb=# insert into person values('21','ddd');
INSERT 0 1
teledb=#
teledb=# insert into person values('21','eee');
INSERT 0 1
teledb=# select
teledb-# deptno,
teledb-# listagg (ename,',') WITHIN GROUP (ORDER BY ENAME)
teledb-# from
teledb-# person
teledb-# group by
teledb-# deptno ;
deptno | listagg
--------+-------------
20 | aaa,bbb,ccc
21 | ddd,eee
(2 rows)