如何从数组 PostgreSQL 的列中获取唯一值

How to get unique values from a column of Arrays PostgreSQL

我正在尝试提取数组列的 DISTINCT 值。

例如,如果我有两行:

{jonathan,michelle}
{jonathan,michael}

输出应该是:

{jonathan,michelle,michael}

输出可以是数组,也可以是"virtual column"没问题。

您可以取消嵌套并返回聚合,忽略重复项:

select array_agg(distinct u.val) new_ar
from mytable t
cross join lateral unnest(t.ar) as u(val)

请注意,这不能保证元素在最终数组中出现的顺序(有选项,但您没有在这方面指定您想要的)。

Demo on DB Fiddle:

| new_ar                      |
| :-------------------------- |
| {jonathan,michael,michelle} |

以下似乎可以解决问题:

SELECT DISTINCT(unnest(tbl.ar)) FROM tbl