WMSYS.WM_CONCAT: 依賴WMSYS 用戶,不一樣oracle環境時可能用不了,返回類型爲CLOB,可用substr截取長度後to_char轉化爲字符類型oracle
LISTAGG : 11g2才提供的函數,不支持distinct,拼接長度不能大於4000,函數返回爲varchar2類型,最大長度爲4000.函數
listagg與vmsys.vm_concat:能夠實現行轉成列,並以逗號分開的效果。spa
區別:listagg是11.2新增的函數,且該函數能夠實現組內的排序orm
--vmsys.vm_concat函數使用 以下所示,按部門進行分組,同一組的在一行中用逗號隔開
SELECT deptno, wmsys.wm_concat(ename) FROM emp GROUP BY deptno;
排序
listagg函數:it
sys_connect_by_path函數:io
SELECT sys_connect_by_path(ename, ',')
FROM (SELECT ename, deptno, rownum rn FROM emp)
START WITH rn = 1
CONNECT BY rn = rownum;
im
--substr從第二個開始截取,去掉第一個逗號
SELECT substr(sys_connect_by_path(ename, ','), 2)
FROM (SELECT ename, deptno, rownum rn FROM emp ORDER BY deptno)
START WITH rn = 1
CONNECT BY rn = rownum;
margin
--值截取最大的最後一條記錄(與vmsys.vm_concat函數有點不一樣,不能按某個字段進行group by,而是不斷的累積)
SELECT max(substr(sys_connect_by_path(ename, ','), 2))
FROM (SELECT ename, deptno, rownum rn FROM emp ORDER BY deptno)
START WITH rn = 1
CONNECT BY rn = rownum;
top
--實現與上述max同樣的效果(connect_by_isleaf只取出爲葉節點的記錄)
SELECT substr(sys_connect_by_path(ename, ','), 2),connect_by_isleaf
FROM (SELECT ename, deptno, rownum rn FROM emp ORDER BY deptno)
WHERE connect_by_isleaf = 1
START WITH rn=1
CONNECT BY prior rn = rn-1;