【问题标题】:concatenate the columns using oracle 11g with '-'使用 oracle 11g 与 '-' 连接列
【发布时间】:2016-05-11 13:09:06
【问题描述】:

输入看起来像这样,有人可以告诉我如何获得预期的输出。 (对于每个唯一的客户 ID,必须连接为 start_term-end_term,rate;)

输入:

customer_id   start_term  end_term    rate 
-----------   ----------  --------    ----
101           61          72          0   
101           37          60          0  
101           24          36          0.9  
102           61          72          2.92  
103           24          36          2.92  
104           61          72          0  
104           37          60          0  
104           24          36          0 

预期输出:

customer_id rate  
----------- -------------------------
101         61-72,0;37-60,0;24-36,0.9  
102         61-72,2.92  
103         24-36,2.92  
104         61-72,0;37-60,0;24-26,0  

谢谢

【问题讨论】:

  • 您是否尝试过任何方法,例如在谷歌上搜索解决方案?

标签: sql plsql oracle11g concatenation


【解决方案1】:

你正在寻找listagg(),类似这样:

select customerid,
       listagg(start_term || '-' || end_term || ',' || rate, '; ')
           within group (order by start_term desc) 
from t
group by customerid;

【讨论】:

    【解决方案2】:

    你需要的是LISTAGG函数。

    你可以试试这个:

    SELECT customer_id, LISTAGG(start_term || '-' || end_term || ',' || rate, '; ')
           WITHIN GROUP (ORDER BY rate) AS rate
      FROM table_name
     GROUP BY customer_id;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-04-19
      • 2020-08-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-06
      • 2011-12-08
      相关资源
      最近更新 更多