Concat the second column value if the first column value is same

本小妞迷上赌 提交于 2019-12-17 22:29:00

问题


I have a query like below and listed output of it:

SELECT DISTINCT TRACKING_NUM,TITLE_OF_DOC_SEC 
FROM some_table  
WHERE  TRACKING_NUM IS NOT NULL;

o/p:

TRACKING_NUM   TITLE_OF_DOC_SEC
007            Email Flow
007            Test Bug 53306
007            Title 1119
007            Title Test
007            test bug
009            1156
089            Title 21173
098            test Doc Section

I want to re write the query so that get an output to be like this:

TRACKING_NUM    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

can anyone help?


回答1:


Use Listagg() in 11g or WM_Concat() in 10g:

   SELECT LISTAGG(TITLE_OF_DOC_SEC, ',') WITHIN GROUP (ORDER BY TRACKING_NUM) AS TITLE_OF_DOC_SEC 
     FROM your table
    WHERE....

   SELECT WM_CONCAT(TITLE_OF_DOC_SEC) AS TITLE_OF_DOC_SEC
     FROM your table
    WHERE....



回答2:


If you are using Oracle 11g+, then you can use LISTAGG():

SELECT TRACKING_NUM,
  LISTAGG(TITLE_OF_DOC_SEC, ', ') WITHIN GROUP (ORDER BY TRACKING_NUM) AS TITLE_OF_DOC_SEC 
FROM some_table  
WHERE  TRACKING_NUM IS NOT NULL
GROUP BY TRACKING_NUM;

See SQL Fiddle with Demo




回答3:


in 11g Listagg is the best option:

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 you can use wm_concat (but be aware this is not documented):

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

or even the model clause:

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


来源:https://stackoverflow.com/questions/15414809/concat-the-second-column-value-if-the-first-column-value-is-same

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!