How to concatenate text from multiple rows into a single text string in Oracle server?

自古美人都是妖i 提交于 2019-12-12 06:54:40

问题


I have a table with data like this:

ID, END_DATE,   CLOSE_DATE

1,  2018-05-18, 2018-05-15 

2,  2018-05-18, 2018-05-14

3,  2018-05-18, 2018-05-11

4,  2018-05-18, 2018-05-10

I need the output when i query in Oracle server

 END_DATE    CLOSE_DATE                                    ID

 2018-05-18  [2018-05-15,2018-05-14,2018-05-11,2018-05-10] [1,2,3,4]

I have tried to use listagg, but its working either for Id or Close Date but not both.

select (listagg(CLOSE_DATE,',') within group (order by CLOSE_DATE DESC)) CLOSE_DATE
     , END_DATE
  from TBL_S_PLCLOSEDATE
 where D_END='18-MAY-2018'

Please let me know how to write the sql for the same in Oracle server.


回答1:


Have you tried this?

select end_date,
       listagg(CLOSE_DATE, ',') within group (order by CLOSE_DATE DESC) as close_dates,
       listagg(id, ',') within group (order by id) as ids
from TBL_S_PLCLOSEDATE
where D_END = date '2018-05-18'
group by end_date;


来源:https://stackoverflow.com/questions/51322807/how-to-concatenate-text-from-multiple-rows-into-a-single-text-string-in-oracle-s

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