【问题标题】:Concatenate strings in a column in PostgreSQL在 PostgreSQL 的列中连接字符串
【发布时间】:2020-12-04 18:51:32
【问题描述】:

我正在尝试转换下表:

id  another_id_1 another_id_2  remarks
1   34.         151.         good
2.  34.         151.         okay
3.  34.         152.         bad
4.  34.         153.         very good
5.  34          153          okay
6.  34          154          good
7.  34          155          bad

进入

another_id_1 another_id_2 remarks
34.          151.         good, okay
34.          152.         bad
34.          153          very good, okay
34.          154          good
34           155          bad

下表使用postgresql语句:

有没有办法可以实现,我尝试过的似乎都没有用

【问题讨论】:

  • 请解释一下逻辑,不清楚。
  • @GordonLinoff 我编辑了这个问题,所以表格的格式很好。这样更好吗?
  • 第一行good, okay, bad中的bad是哪来的?我看不到任何 (34, 151) 包含 bad 作为备注的行
  • @a_horse_with_no_name 对此我感到非常抱歉,我刚刚再次编辑了问题

标签: sql postgresql string-aggregation


【解决方案1】:

虽然与你的数据不一致,但我认为你想要聚合:

select another_id_1, another_id_2, 
       string_agg(remarks, ', ' order by id) as remarks
from t
group by another_id_1, another_id_2;

【讨论】:

  • 感谢您的快速响应,我刚刚尝试过,但似乎没有按预期对它们进行分组。我还编辑了我的问题,所以表格的格式很好。如果你有时间可以再看看吗?谢谢
  • 这就是答案,而且效果很好。我马上买你的书。我想像你一样了解sql
猜你喜欢
  • 2013-11-09
  • 1970-01-01
  • 2010-10-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-04-24
  • 2010-09-07
相关资源
最近更新 更多