Thursday, September 30, 2010

String Aggregation - 对文字字段进行统计

在SQL里面最基本的统计是基于Grouping上面的。但大部分的统计函数是对于数字类型进行统计计算的。如何对文字字段进行统计计算呢。有一些统计计算如下:
  • 连接字符串 (String Concatenating)
如下表:
SQL> select * from emp;
    DEPTNO ENAME
---------- ----------------------------------------
        20 SMITH
        30 ALLEN
        30 WARD
        20 JONES
        30 MARTION
        30 BLAKE
        10 CLARK
        20 SCOTT
        10 KING
        30 TURNER
        20 ADAMS
        30 JAMES
        20 FORD
        10 MILLER
14 rows selected.

需要生成一个表为:
     DEPTNO EMPLOYEES
---------- --------------------------------------------------
        10 CLARK,KING,MILLER
        20 SMITH,FORD,ADAMS,SCOTT,JONES
        30 ALLEN,BLAKE,MARTION,TURNER,JAMES,WARD
  • 与上述操作相反的操作,把一个用分隔符分开的字符串分解为多个行。

  • 其他

5 comments:

  1. 一些参考:
    http://www.oracle-base.com/articles/misc/StringAggregationTechniques.php

    http://www.simple-talk.com/sql/t-sql-programming/concatenating-row-values-in-transact-sql/

    http://forums.oracle.com/forums/thread.jspa?threadID=691381

    http://forums.oracle.com/forums/thread.jspa?threadID=901462

    http://asktom.oracle.com/pls/asktom/f?p=100:11:0::::P11_QUESTION_ID:2196162600402

    ReplyDelete
  2. Oracle 11g Release 2:
    SQL> SELECT deptno, LISTAGG(ename, ',') WITHIN GROUP (ORDER BY ename) AS employees
    2 FROM emp
    3 GROUP BY deptno;

    DEPTNO EMPLOYEES
    ---------- --------------------------------------------------
    10 CLARK,KING,MILLER
    20 ADAMS,FORD,JONES,SCOTT,SMITH
    30 ALLEN,BLAKE,JAMES,MARTION,TURNER,WARD

    ReplyDelete
  3. wmsys.wm_concat:
    SQL> select deptno, wm_concat(ename) as employees from emp group by deptno;

    DEPTNO EMPLOYEES
    ---------- --------------------------------------------------
    10 CLARK,KING,MILLER
    20 SMITH,FORD,ADAMS,SCOTT,JONES
    30 ALLEN,BLAKE,MARTION,TURNER,JAMES,WARD

    ReplyDelete
  4. LISTAGG与wmsys.wm_concat的区别:
    1. wmsys.wm_concat 允许distinct选项。
    而ListAgg不允许
    create table diffT(sortKey,Val) as
    select 1,'aa' from dual union all
    select 2,'bb' from dual union all
    select 3,'aa' from dual union all
    select 4,'dd' from dual;

    col concatV for a20

    select wmsys.wm_concat(distinct Val) as concatV from diffT;

    concatV
    --------
    aa,bb,dd

    2. ListAgg 可以设置连接顺序。
    wmsys.wm_concat 不允许。
    select ListAgg(Val,',')
    within group(order by sortKey desc) as concatV
    from diffT;

    CONCATV
    ------------
    dd,aa,bb,aa

    3. ListAgg 可指定分隔符。
    wmsys.wm_concat 不可以。
    select ListAgg(Val,'***')
    within group(order by sortKey desc) as concatV
    from diffT;

    CONCATV
    -----------------
    dd***aa***bb***aa

    4. wmsys.wm_concat 允许在OLAP函数中使用order by。ListAgg不允许。
    ListAgg 只允许不带order by的OLAP函数。
    select sortKey,wmsys.wm_concat(Val)
    over(order by sortKey) as concatV
    from diffT;

    SORTKEY CONCATV
    ------- -----------
    1 aa
    2 aa,bb
    3 aa,bb,aa
    4 aa,bb,aa,dd

    5. wmsys.wm_concat 可使用KEEP。
    ListAgg 不可。
    select wmsys.wm_concat(Val)
    Keep(Dense_Rank First order by Val) as concatV
    from diffT;

    CONCATV
    -------
    aa,aa

    ReplyDelete
  5. 关于SQL Server 的参考:
    http://www.projectdmx.com/tsql/rowconcatenate.aspx

    ReplyDelete