-- 查询部门为20的员工列表
SELECT t.DEPTNO,t.ENAME FROM SCOTT.EMP t where t.DEPTNO = '20' ;
2.使用 listagg() WITHIN GROUP () 将多行合并成一行
SELECT
T .DEPTNO,
listagg (T .ENAME, ',') WITHIN GROUP (ORDER BY T .ENAME) names
FROM
SCOTT.EMP T
WHERE
T .DEPTNO = '20'
GROUP BY
T .DEPTNO
函数问题:
1、由于在使用中listagg中的字段值拼接起来后长度超过4000
经查询发现是listagg结果长度限制为Varchar 4000,帮改为以下方式解决:
使用xmlagg:
xmlagg(xmlparse(content 合并字段||’,’ wellformed) order by 排序字段).getclobval()
mysql的替代函数:
select deptNo,group_concat(name) as names from scott.emt group by deptno