我有一个递归查询,其中获取数组行,如下所示。如何将所有行合并为一行中的一个数组并删除重复项?排序并不重要。

--my_column--
"{431}"
"{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(select unnest(my_column) from my_table

谢谢

最佳答案

使用array_agg()distinct和(不需要)从order by开始:

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)

09-25 19:27