如何从string_agg重新排列值

时间:2017-03-24 20:39:53

标签: postgresql

  

如何重新排列或简化string_agg中的值?

     

例如,从该值1 | 2 | 3 | 4 | 5 | K到此值K-5或从该值2 | 3 | 5到2-5 ..

SUBSTRING(string_agg(DISTINCT (trim(leading '0' from sgl.short_name)),'|' )FOR 11) as GRADE

1 个答案:

答案 0 :(得分:0)

您不需要string_agg,但需要一个窗口功能:

SELECT DISTINCT
       CAST (first_value(short_name) OVER w AS text)
          || '-'
          || CAST(last_value(short_name) OVER w AS text)
FROM sgl
WINDOW w AS (PARTITION BY somecol ORDER BY short_name);

由于缺乏样本数据而未经测试。