【发布时间】:2019-11-07 17:16:15
【问题描述】:
以下答案的更新/注释
我的目标列上的pg_typeof() 是“regtype”,而不是“text”...
所以下面的人为例子是一个红鲱鱼。下面的@richyen 正确回答了它。对于那些表格不是文本的人,需要转换为文本,这是下面@a_horse_with_no_name 接受的答案
来自表myschema.thing:
id | fruit | color | owner_id
---+--------+--------+---------
1 | apple | red | 100
2 | banana | yellow | 100
3 | tomato | red | 500
4 | grape | purple | 200
我正在尝试得到结果:
colors
------------------
red,yellow,purple
基于此页面:https://www.postgresql.org/docs/11/functions-string.html
我试过了:
select concat_ws(',', distinct(color)) as colors
from myschema.thing
但它不起作用。我哪里错了?提前致谢。
【问题讨论】:
-
您的表被称为
thing并且您还有一个名为thing的列,这有点令人困惑 -
@richyen ^谢谢。固定的。这是我为了说明我的问题而编造的一个通用示例。
标签: sql postgresql string-aggregation