Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/77.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在postgresql中连接数据_Sql_Postgresql_Concatenation - Fatal编程技术网

在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,我只想显示一次

我有这样一个表数据:

我想在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,我只想显示一次


请帮帮我

使用
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

按项目排序
无效,因为
按项目分组
,这意味着所有聚合的值在
项目
列中将具有相同的值。
按项目排序
无效,因为
按项目分组
,这意味着所有聚合的值在列
项目