Created
December 21, 2016 16:41
-
-
Save ryanguill/6c0e82dc7dee9d025bd27ad2abc274b9 to your computer and use it in GitHub Desktop.
in postgres, there currently isnt a function to combine two arrays and remove duplicates - this will do that.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CREATE OR REPLACE FUNCTION mergeArrays (a1 ANYARRAY, a2 ANYARRAY) RETURNS ANYARRAY AS $$ | |
SELECT ARRAY_AGG(x ORDER BY x) | |
FROM ( | |
SELECT DISTINCT UNNEST($1 || $2) AS x | |
) s; | |
$$ LANGUAGE SQL STRICT; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It looks like it works to me - an empty array you must define as the kind of data type in the array, but it works.
https://dbfiddle.uk/?rdbms=postgres_9.6&fiddle=ae6385a5b138c262008de6f0b35986e5