在 11g Listagg 中是最好的选择:
SQL> select tracking_num,
2 listagg(title_of_doc_sec,',') within group (order by title_of_doc_sec) title_of_doc_sec
3 from (select distinct tracking_num , title_of_doc_sec from some_table)
4 group by tracking_num;
TRA TITLE_OF_DOC_SEC
--- ----------------------------------------------------------------------
007 Email Flow,Test Bug 53306,Title 1119,Title Test,test bug
009 1156
089 Title 21173
098 test Doc Section
10g 你可以使用wm_concat
(但请注意这没有记录):
SQL> select tracking_num,
2 wm_concat(title_of_doc_sec) title_of_doc_sec
3 from (select distinct tracking_num , title_of_doc_sec from some_table)
4 group by tracking_num;
TRA TITLE_OF_DOC_SEC
--- ----------------------------------------------------------------------
007 Title Test,test bug,Title 1119,Email Flow,Test Bug 53306
009 1156
089 Title 21173
098 test Doc Section
甚至是示范条款:
SQL> with data as (select distinct tracking_num , title_of_doc_sec from some_table)
2 select tracking_num, title_of_doc_sec
3 from (select *
4 from data
5 model
6 partition by (tracking_num)
7 dimension by (row_number() over (partition by tracking_num order by title_of_doc_sec) rn)
8 measures (title_of_doc_sec t, cast(null as varchar2(4000)) title_of_doc_sec,
9 count(*) over (partition by tracking_num) cnt)
10 rules(
11 title_of_doc_sec[any] = case when t[cv() - 1 ] is null
12 then t[cv()]
13 else title_of_doc_sec[cv()-1]||', '|| t[cv()]
14 end
15 ))
16 where cnt = rn;
TRA TITLE_OF_DOC_SEC
--- ----------------------------------------------------------------------
007 Email Flow, Test Bug 53306, Title 1119, Title Test, test bug
009 1156
089 Title 21173
098 test Doc Section