【问题标题】:How to concatenate text from multiple rows into a single text string in Oracle server?如何在 Oracle 服务器中将多行文本连接成单个文本字符串?
【发布时间】:2018-12-21 16:45:24
【问题描述】:

我有一张表格,里面有这样的数据:

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

我在Oracle服务器中查询时需要输出

 END_DATE    CLOSE_DATE                                    ID

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

我曾尝试使用 listagg,但它适用于 Id 或 Close Date,但不能同时适用。

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'

请让我知道如何在 Oracle 服务器中编写相同的 sql。

【问题讨论】:

    标签: sql oracle concatenation string-aggregation


    【解决方案1】:

    你试过了吗?

    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;
    

    【讨论】:

    • 有没有办法在 Close_dates 中将日期格式从 DD-MON-YYY (16-JUN-2018) 更改为 16/06/2018?
    • @Vasanth 。 . .你会使用to_char()
    猜你喜欢
    • 2010-09-16
    • 1970-01-01
    • 2021-08-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多