拆分数组,只提取唯一值,然后重新合并到Postgresql中的数组

时间:2017-11-16 10:46:23

标签: postgresql

我有一个使用此代码分组的数据集:

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        |
+----+--------+-----------------------------------------+

1 个答案:

答案 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;

但最好在分组时聚合不同的值。