Skip to content

Instantly share code, notes, and snippets.

@yancya
Last active December 21, 2015 16:59
Show Gist options
  • Save yancya/6337654 to your computer and use it in GitHub Desktop.
Save yancya/6337654 to your computer and use it in GitHub Desktop.
PostgreSQL で配列と配列の差集合の配列を返す関数
--
-- SELECT array_minus(array[1,2,3], array[1,2]); -> {3}
--
CREATE OR REPLACE FUNCTION pg_temp.array_minus (anyarray, anyarray) RETURNS anyarray AS
$$
SELECT array_agg(t.unnest)
FROM ((SELECT UNNEST($1))
EXCEPT
(SELECT UNNEST($2)))
AS t;
$$
LANGUAGE SQL;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment