Re: Subtract one array from another, both with non-unique elements - Mailing list pgsql-general

From Victor Yegorov
Subject Re: Subtract one array from another, both with non-unique elements
Date
Msg-id CAGnEbojj+yONZiFew-aZaPEmDPOwNXqnwBqDjoAcKjDMApZTmQ@mail.gmail.com
Whole thread Raw
In response to Re: Subtract one array from another, both with non-unique elements  (Alexander Farber <alexander.farber@gmail.com>)
List pgsql-general
2016-03-08 15:59 GMT+02:00 Alexander Farber <alexander.farber@gmail.com>:
Here 1-pass version, if you have improvement suggestions, you are welcome -

My variant:

CREATE OR REPLACE FUNCTION arrexcept(anyarray, anyarray) RETURNS anyarray AS $arrexcept$
  SELECT array_agg(un) FROM (
    SELECT un, row_number() OVER (PARTITION BY un ORDER BY ord) id FROM unnest($1) with ordinality AS t(un, ord)
    EXCEPT
    SELECT un, row_number() OVER (PARTITION BY un ORDER BY ord) id FROM unnest($2) with ordinality AS t(un, ord)
  ) x;
$arrexcept$ LANGUAGE sql;

postgres=# select arrexcept(ARRAY['A','A','B','B','C'], ARRAY['A','B']);
 arrexcept
-----------
 {A,B,C}
(1 row)


But it doesn't preserves the order of the elements, not sure if this is important.



--
Victor Y. Yegorov

pgsql-general by date:

Previous
From: Adrian Klaver
Date:
Subject: Re: Exclude pg_largeobject form pg_dump
Next
From: Andreas Joseph Krogh
Date:
Subject: Re: Exclude pg_largeobject form pg_dump