如何使用 postgres 获取不同的数组元素?

How to get distinct array elements with postgres?

我在 postgres 中有一个包含重复值的数组。例如:

SELECT cardinality(string_to_array('1,2,3,4,4', ',')::int[]) as foo
=> "foo"=>"5"

我想获取独特的元素,例如:

SELECT cardinality(uniq(string_to_array('1,2,3,4,4', ',')::int[])) as foo
=> -- No function matches the given name and argument types. You might need to add explicit type casts.

我可以在不使用 UNNEST 的情况下在 postgres 中获取数组的唯一元素吗?

对于整数数组使用 intarray extension:

create extension if not exists intarray;
select cardinality(uniq(string_to_array('1,2,3,4,4', ',')::int[])) as foo

或函数

create or replace function public.array_unique(arr anyarray)
    returns anyarray
    language sql
as $function$
    select array_agg(distinct elem)
    from unnest(arr) as arr(elem) 
$function$;

对于任何数组。您可以轻松修改函数以保留数组元素的原始顺序:

create or replace function public.array_unique_ordered(arr anyarray)
    returns anyarray
    language sql
as $function$
    select array_agg(elem order by ord)
    from (
        select distinct on(elem) elem, ord
        from unnest(arr) with ordinality as arr(elem, ord)
        order by elem, ord
        ) s
$function$;

示例:

with my_data(arr) as (values ('{d,d,a,c,b,b,a,c}'::text[]))
select array_unique(arr), array_unique_ordered(arr)
from my_data

 array_unique | array_unique_ordered
--------------+----------------------
 {a,b,c,d}    | {d,a,c,b}
(1 row)

我更喜欢这种语法(大约快 5%)

create or replace function public.array_unique(arr anyarray)
returns anyarray as $body$
    select array( select distinct unnest() )
$body$ language 'sql';

使用:

select array_unique(ARRAY['1','2','3','4','4']);

离开@klin 接受的答案,我修改了它以在仅选择不同值的过程中删除空值。

create or replace function public.array_unique_no_nulls(arr anyarray)
returns anyarray
language sql
as $function$
select array_agg(distinct a)
from (
    select unnest(arr) a 
) alias
where a is not null
$function$;