Oracle LISTAGG排重方法,oraclelistagg排重
1.使用Regex進行排重
SELECT regexp_replace('2,2,3,4,5 ', '([^,]+)(,\1)+', '\1') AS col FROM dual
查詢結果:
SQL> SELECT regexp_replace('2,2,3,4,5 ', '([^,]+)(,\1)+', '\1') AS col 2 FROM dual 3 ; COL--------2,3,4,5
可以對字串進行排重
2. 使用上面的Regex對listagg進行排重
SELECT listagg(t.deptno, ',') within GROUP(ORDER BY t.deptno) deptno, regexp_replace((listagg(t.deptno, ',') within GROUP(ORDER BY t.deptno)), '([^,]+)(,\1)+', '\1') bl_number FROM scott.emp t
執行結果:
SQL> SELECT listagg(t.deptno, ',') within GROUP(ORDER BY t.deptno) deptno, 2 regexp_replace((listagg(t.deptno, ',') within GROUP(ORDER BY t.deptno)), '([^,]+)(,\1)+', '\1') bl_number 3 FROM scott.emp t 4 ; DEPTNO BL_NUMBER-------------------------------------------------------------------------------- --------------------------------------------------------------------------------10,10,10,20,20,20,20,20,30,30,30,30,30,30 10,20,30
成功對listagg排重。