Created
March 4, 2024 11:00
-
-
Save Jeetah/c1e03e1c1c80e4cb47d713bddd095907 to your computer and use it in GitHub Desktop.
SQL function to select distinct elements from an Postgres array
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 FUNCTION array_distinct( | |
anyarray, -- input array | |
boolean DEFAULT false -- flag to ignore nulls | |
) RETURNS anyarray AS $f$ | |
SELECT array_agg(DISTINCT x) | |
FROM unnest($1) t(x) | |
WHERE CASE WHEN $2 THEN x IS NOT NULL ELSE true END; | |
$f$ LANGUAGE SQL IMMUTABLE; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment