How to concatenate multiple rows order by sequence in Oracle10g

前端 未结 1 588
我寻月下人不归
我寻月下人不归 2021-01-22 17:16

If I have a data like this:

GROUP  |  SEQUENCE  |  COMMAND
------------------------------
ONE    |     3      |  MESSAGE
ONE             


        
相关标签:
1条回答
  • 2021-01-22 18:19

    Never use WM_CONCAT. Read Why not use WM_CONCAT function in Oracle?

    See this topic https://stackoverflow.com/a/28758117/3989608.

    It is undocumented, and any application which relies on WM_CONCAT will not work once upgraded to 12c because it has been removed from the latest 12c version.

    There are many ways of doing string-aggregation, depending on the database version. See few examples below:

    11gR2

    Use LIASTAGG:

    SQL> SELECT grp,
      2    listagg(command, ',') WITHIN GROUP(
      3  ORDER BY seq) command
      4  FROM t
      5  GROUP BY grp;
    
    GRP COMMAND
    --- --------------------------------------------------------------------------------------------
    ONE <?xml version=1.0 encoding=UTF-8?>,<message1>MESSAGE</message1>,<message2>MESSAGE</message2>
    TWO <?xml version=1.0 encoding=UTF-8?>,<message2>MESSAGE</message2>,<message9>MESSAGE</message9>
    
    SQL>
    

    9i and up

    Use ROW_NUMBER() and SYS_CONNECT_BY_PATH:

    SQL> SELECT grp,
      2         LTRIM(MAX(SYS_CONNECT_BY_PATH(command,','))
      3         KEEP (DENSE_RANK LAST ORDER BY seq),',') command
      4  FROM   (SELECT grp,
      5                 command,
      6                 seq,
      7                 ROW_NUMBER() OVER (PARTITION BY grp ORDER BY seq) AS curr,
      8                 ROW_NUMBER() OVER (PARTITION BY grp ORDER BY seq) -1 AS prev
      9          FROM   t)
     10  GROUP BY grp
     11  CONNECT BY prev = PRIOR curr AND grp = PRIOR grp
     12  START WITH curr = 1;
    
    GRP COMMAND
    --- --------------------------------------------------------------------------------------------
    ONE <?xml version=1.0 encoding=UTF-8?>,<message1>MESSAGE</message1>,<message2>MESSAGE</message2>
    TWO <?xml version=1.0 encoding=UTF-8?>,<message2>MESSAGE</message2>,<message9>MESSAGE</message9>
    
    SQL>
    
    0 讨论(0)
提交回复
热议问题