Postgres Arrays行聚合

时间:2017-02-19 00:25:36

标签: arrays postgresql aggregation

我有一个递归查询,其中我获得了如下所示的数组行。我怎么可能将所有行合并为一行中的一个数组并删除重复项?订购并不重要。

select array(select unnest(my_column) from my_table

我尝试了下面的查询,但我得到一个空整数[]列

GC.Collect(0);

感谢

2 个答案:

答案 0 :(得分:2)

array_agg()使用distinct order byunnest() {必填} with my_table(my_column) as ( values ('{431}'::int[]), ('{431,33}'), ('{431,60}'), ('{431,28}'), ('{431,1}'), ('{431,226}'), ('{431,38}'), ('{431,226,229}'), ('{431,226,227}'), ('{431,226,235}'), ('{431,226,239}'), ('{431,226,241}') ) select array_agg(distinct elem order by elem) from my_table, lateral unnest(my_column) elem; array_agg --------------------------------------------- {1,28,33,38,60,226,227,229,235,239,241,431} (1 row)

rand()

答案 1 :(得分:2)

没有lateral subquery的另一种解决方案:

select array_agg(distinct val) from
  (select unnest(my_column) as val from my_table) x;