Skip to content

Instantly share code, notes, and snippets.

@jczaplew
Created March 19, 2015 15:02
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jczaplew/4512e3f62e30490f2a00 to your computer and use it in GitHub Desktop.
Save jczaplew/4512e3f62e30490f2a00 to your computer and use it in GitHub Desktop.
Postgres array union of result set
CREATE TABLE test (
some_arrays int[]
);
INSERT INTO test (some_arrays) VALUES (array[1,2,3]), (array[3,4,5]), (array[5,6,7]), (array[7,8,9]);
WITH first_query AS (
SELECT 1 AS arbitrary_group_by, unnest(some_arrays) AS my_numbers
FROM test
)
SELECT array_agg(distinct my_numbers)
FROM first_query
GROUP BY arbitrary_group_by;
-> Returns {1,2,3,4,5,6,7,8,9}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment