-- 查询部门为20的员工列表
SELECT t.DEPTNO,t.ENAME FROM SCOTT.EMP t where t.DEPTNO = '20';
SELECT T.DEPTNO, LISTAGG (T.ENAME, ',') WITHIN GROUP (ORDER BY T.ENAME) AS NAMES FROM SCOTT.EMP T WHERE T.DEPTNO = '20' GROUP BY T.DEPTNO;
SELECT T.DEPTNO, LISTAGG(T.ENAME,',') WITHIN GROUP (ORDER BY T.ENAME) OVER(PARTITION BY T.DEPTNO) as ENAME FROM SCOTT.EMP T WHERE T.DEPTNO = '20';
4、XMLAGG
语句一:结果中的聚合字段是CLOB类型,是文本字符。处理400多万条数据,运行时间是20分钟。
SELECT T.DEPTNO,LTRIM(XMLAGG(XMLELEMENT(T,',',T.ENAME)ORDER BY T.ENAME).EXTRACT('//text()').GETCLOBVAL(),',') AS NAMES FROM SCOTT.EMP T WHERE T.DEPTNO=20 GROUP BY T.DEPTNO;
如下:
语句二:结果汇总的聚合字段也是clob类型,不过是hex字节;处理400多万条数据,运行时间是56秒
SELECT T.DEPTNO, RTRIM(XMLAGG(XMLPARSE(CONTENT T.ENAME || ','WELLFORMED) ORDER BY T.ENAME).GETCLOBVAL(),',') AS NAMES FROM SCOTT.EMP T WHERE T.DEPTNO=20 GROUP BY T.DEPTNO;
效果:
文章来源: https://blog.csdn.net/makai5521/article/details/90709712