在postgresql中连接数据
我有这样一个表数据: 我想在postgresql中连接以下数据: 我使用的查询如下,但有一个小问题:在postgresql中连接数据,sql,postgresql,concatenation,Sql,Postgresql,Concatenation,我有这样一个表数据: 我想在postgresql中连接以下数据: 我使用的查询如下,但有一个小问题: select Item, array_to_string(array_agg(Component) ,', ') AS Component, array_to_string(array_agg(Seller) ,', ') AS Seller from tablename group by Item 它的输出如下: 对于A2项,它显示了两次C89,我只想显示一次
select
Item,
array_to_string(array_agg(Component) ,', ') AS Component,
array_to_string(array_agg(Seller) ,', ') AS Seller
from tablename
group by Item
它的输出如下:
对于A2项,它显示了两次C89,我只想显示一次
请帮帮我 使用
distinct
,您也不需要array\u agg()
:
使用
distinct
,您也不需要array\u agg()
:
这应该行得通,有时你需要告诉它如何对不同的数据进行排序
select
Item,
array_to_string(array_agg(DISTINCT Component ORDER BY Item DESC ) ,', ') AS Component,
array_to_string(array_agg((DISTINCT Seller ORDER BY Item DESC) ,', ') AS Seller
from tablename
group by Item
这应该行得通,有时你需要告诉它如何对不同的数据进行排序
select
Item,
array_to_string(array_agg(DISTINCT Component ORDER BY Item DESC ) ,', ') AS Component,
array_to_string(array_agg((DISTINCT Seller ORDER BY Item DESC) ,', ') AS Seller
from tablename
group by Item
按项目排序
无效,因为按项目分组
,这意味着所有聚合的值在项目
列中将具有相同的值。按项目排序
无效,因为按项目分组
,这意味着所有聚合的值在列项目