How to get distinct array elements with postgres?

后端 未结 3 693
忘掉有多难
忘掉有多难 2021-01-14 00:23

I have an array with duplicate values in postgres. For example:

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


        
3条回答
  •  爱一瞬间的悲伤
    2021-01-14 00:38

    For integer arrays use intarray extension:

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

    or the function

    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$;
    

    for any array. You can easily modify the function to preserve the original order of the array elements:

    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$;
    

    Example:

    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)
    

提交回复
热议问题