Re: Subtract one array from another, both with non-unique elements

Поиск
Список
Период
Сортировка
От Victor Yegorov
Тема Re: Subtract one array from another, both with non-unique elements
Дата
Msg-id CAGnEbojj+yONZiFew-aZaPEmDPOwNXqnwBqDjoAcKjDMApZTmQ@mail.gmail.com
обсуждение исходный текст
Ответ на Re: Subtract one array from another, both with non-unique elements  (Alexander Farber <alexander.farber@gmail.com>)
Список 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 по дате отправления: