From: | Victor Yegorov <vyegorov(at)gmail(dot)com> |
---|---|
To: | Alexander Farber <alexander(dot)farber(at)gmail(dot)com> |
Cc: | pgsql-general <pgsql-general(at)postgresql(dot)org> |
Subject: | Re: Subtract one array from another, both with non-unique elements |
Date: | 2016-03-08 14:51:07 |
Message-ID: | CAGnEbojj+yONZiFew-aZaPEmDPOwNXqnwBqDjoAcKjDMApZTmQ@mail.gmail.com |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Thread: | |
Lists: | pgsql-general |
2016-03-08 15:59 GMT+02:00 Alexander Farber <alexander(dot)farber(at)gmail(dot)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
From | Date | Subject | |
---|---|---|---|
Next Message | Andreas Joseph Krogh | 2016-03-08 15:04:54 | Re: Exclude pg_largeobject form pg_dump |
Previous Message | Adrian Klaver | 2016-03-08 14:43:37 | Re: Exclude pg_largeobject form pg_dump |