这是数据的格式:
item_name | item_serial | sub_group | conc_stuff | other_data | more_data
----------+-------------+-----------+------------+------------+-----------
foo bar-01-a widget b-3 towel dent
foo bar-02-a widget a-1 42 mouse
foo bar-03-a widget p-1 babel dolphin
foo3 bar-21-f widget f-1 42 marvin
foo3 bar-22-f widget x-1 poetry vogon
我已经让查询按照我想要的方式执行,问题是我需要返回更多数据。
SELECT item_name,
array_to_string(array_agg(conc_stuff), ',') as stuff
FROM dataset
WHERE some_selector = 'X'
GROUP BY item_name
ORDER BY item_name;
我尝试了对我来说似乎简单但合乎逻辑的方法:
SELECT item_name,
item_serial,
sub_group,
array_to_string(array_agg(conc_stuff), ',') as stuff
FROM dataset
WHERE some_selector = 'X'
GROUP BY item_name
ORDER BY item_name;
我需要返回如下所示的内容:
item_name | item_serial | sub_group | stuff
----------+-------------+-----------+-------------
foo bar-01-a widget a-1,b-3,p-1
foo3 bar-21-f widget f-1,x-1,g-5
foo6 bar-81-z widget r-1,d-8,w-0
而不仅仅是这个:
item_name | stuff
----------+--------------
foo a-1,b-3,p-1
foo3 f-1,x-1,g-5
foo6 r-1,d-8,w-0
当我尝试向查询添加其他字段时,我得到:
ERROR: column "stuff.item_serial" must appear in the GROUP BY clause or be used in an aggregate function
但是我不想GROUP BY
item_serial,我只是希望它与聚合一起返回,对吗?
我需要运行子查询吗?我相信这很简单。如果有多种方法,哪种方法最有效?我将连接的一些文本是坐标(大字符串文本)。