eliminate duplicate array values in postgres

后端 未结 8 1085
孤独总比滥情好
孤独总比滥情好 2020-12-01 01:27

I have an array of type bigint, how can I remove the duplicate values in that array?

Ex: array[1234, 5343, 6353, 1234, 1234]

I shou

8条回答
  •  独厮守ぢ
    2020-12-01 01:32

    ... Where the statandard libraries (?) for this kind of array_X utility??

    Try to search... See some but no standard:

    • postgres.cz/wiki/Array_based_functions: good reference!

    • JDBurnZ/postgresql-anyarray, good initiative but needs some collaboration to enhance.

    • wiki.postgresql.org/Snippets, frustrated initiative, but "offcial wiki", needs some collaboration to enhance.

    • MADlib: good! .... but it is an elephant, not an "pure SQL snippets lib".


    Simplest and faster array_distinct() snippet-lib function

    Here the simplest and perhaps faster implementation for array_unique() or array_distinct():

    CREATE FUNCTION array_distinct(anyarray) RETURNS anyarray AS $f$
      SELECT array_agg(DISTINCT x) FROM unnest($1) t(x);
    $f$ LANGUAGE SQL IMMUTABLE;
    

    NOTE: it works as expected with any datatype, except with array of arrays,

    SELECT  array_distinct( array[3,3,8,2,6,6,2,3,4,1,1,6,2,2,3,99] ), 
            array_distinct( array['3','3','hello','hello','bye'] ), 
            array_distinct( array[array[3,3],array[3,3],array[3,3],array[5,6]] );
     -- "{1,2,3,4,6,8,99}",  "{3,bye,hello}",  "{3,5,6}"
    

    the "side effect" is to explode all arrays in a set of elements.

    PS: with JSONB arrays works fine,

    SELECT array_distinct( array['[3,3]'::JSONB, '[3,3]'::JSONB, '[5,6]'::JSONB] );
     -- "{"[3, 3]","[5, 6]"}"
    

    Edit: more complex but useful, a "drop nulls" parameter

    CREATE FUNCTION array_distinct(
          anyarray, -- input array 
          boolean DEFAULT false -- flag to ignore nulls
    ) RETURNS anyarray AS $f$
          SELECT array_agg(DISTINCT x) 
          FROM unnest($1) t(x) 
          WHERE CASE WHEN $2 THEN x IS NOT NULL ELSE true END;
    $f$ LANGUAGE SQL IMMUTABLE;
    

提交回复
热议问题