Skip to content

Instantly share code, notes, and snippets.

@mkwatson
Created October 2, 2020 16:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mkwatson/b0c0a75a9d19e9abc413e128a7b46d53 to your computer and use it in GitHub Desktop.
Save mkwatson/b0c0a75a9d19e9abc413e128a7b46d53 to your computer and use it in GitHub Desktop.
############################
# UNION
############################
WITH t1(v) AS
(VALUES (1), (1), (2)),
t2(v) AS
(VALUES (2), (2), (3))
(SELECT DISTINCT v FROM t1)
UNION
(SELECT DISTINCT v FROM t2)
# reults
# v
# 1 2
# 2 1
# 3 3
############################
# UNION ALL
############################
WITH t1(v) AS
(VALUES (1), (1), (2)),
t2(v) AS
(VALUES (2), (2), (3))
(SELECT DISTINCT v FROM t1)
UNION ALL
(SELECT DISTINCT v FROM t2)
# reults
# v
# 1 3
# 2 1
# 3 2
# 3 2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment