Jangan pernah menggunakan WM_CONCAT
. Baca Mengapa tidak menggunakan fungsi WM_CONCAT di Oracle?
Lihat topik ini https://stackoverflow.com/a/28758117/3989608 .
Itu tidak berdokumen, dan aplikasi apa pun yang bergantung pada WM_CONCAT
tidak akan berfungsi setelah ditingkatkan ke 12c
karena telah dihapus dari versi 12c terbaru.
Ada banyak cara untuk melakukan agregasi string , bergantung pada versi basis data . Lihat beberapa contoh di bawah ini:
11gR2
Gunakan 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 dan lebih tinggi
Gunakan ROW_NUMBER()
dan 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>