Oracle LISTAGG排重方法,oraclelistagg排重

來源:互聯網
上載者:User

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排重。



相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.