我有一个使用此代码分组的数据集:
select
array_to_string(array_agg(DISTINCT "Categories"), ',') as "Categories", "Name", ROW_NUMBER() OVER() as "ID"
from data1
group by "Name"
它看起来像这样:
+----+--------+-----------------------------------------+
| ID | Name | Categories |
+----+--------+-----------------------------------------+
| 1 | Class1 | Barry, Steve, Luke, Barry, Barry, Luke |
+----+--------+-----------------------------------------+
| 2 | Class2 | Luke, Barry, Steve |
+----+--------+-----------------------------------------+
| 3 | Class3 | Gerald, Jacqueline, David, Barry, Barry |
+----+--------+-----------------------------------------+
我需要在"类别"中只有唯一值。但是,在第一行中,数据库将Barry, Steve, Luke
视为字符串,Barry, Luke
为字符串,因此使用DISTINCT
并不能减少数量Barry
秒。
我们需要使用逗号分隔符拆分字符串,然后重新聚合它。
输出应如下所示:
+----+--------+-----------------------------------------+
| ID | Name | Categories |
+----+--------+-----------------------------------------+
| 1 | Class1 | Barry, Steve, Luke |
+----+--------+-----------------------------------------+
| 2 | Class2 | Luke, Barry, Steve |
+----+--------+-----------------------------------------+
| 3 | Class3 | Gerald, Jacqueline, David, Barry |
+----+--------+-----------------------------------------+
答案 0 :(得分:0)
您可以使用此类
查询提取唯一值select
ID,
Name,
(
select string_agg(c, ',')
from (
select
distinct trim(unnest(string_to_array(Categories, ',')))
) t (c)
) as Categories
from your_table_name;
但最好在分组时聚合不同的值。