Change separator of WM_CONCAT function of Oracle 11gR2

后端 未结 4 1378
南方客
南方客 2020-12-11 05:23

Normally, WM_CONCAT is an aggregate function that return values from table separated by comma like here.

Suppose I have a table foo like this:



        
4条回答
  •  时光说笑
    2020-12-11 05:28

    You might want to use LISTAGG.

    SELECT col_id, 
           LISTAGG(col_text, '|') WITHIN GROUP (ORDER BY col_text) text
      FROM table1
     GROUP BY col_id
    

    Output:

    | COL_ID |            TEXT |
    ----------------------------
    |    111 | This|a|is|test. |
    

    SQLFiddle

    UPDATE If you need to get distinct text values in a list

    SELECT col_id, 
           LISTAGG(col_text, '|')
             WITHIN GROUP (ORDER BY col_text) text
      FROM 
    (
      SELECT DISTINCT col_id, col_text
        FROM table1
    )
     GROUP BY col_id
    

    SQLFiddle

提交回复
热议问题