patternsqlMinor
Select distinct multiple columns with one result column
Viewed 0 times
distinctresultcolumnscolumnwithonemultipleselect
Problem
I have a table like the following:
and I would like to do something like
but I do not know if it makes sense.
Maybe with an index over each of the three column I can simply select independently the three columns like that:
But I am not sure about the performance.
Actually I have rows like
And I expect as a result
Any recommendation and good example?
CREATE TABLE aschema.atable
(
id BIGSERIAL,
col_a aschema.anenum,
col_b aschema.anenum,
col_c aschema.anenum
)and I would like to do something like
SELECT DISTINCT unnest(array_agg(col_a, col_b, col_c)) as anenum_value
FROM aschema.atable
WHERE anenum_value IS NOT NULLbut I do not know if it makes sense.
Maybe with an index over each of the three column I can simply select independently the three columns like that:
SELECT DISTINCT value FROM (
SELECT col_a as value
FROM aschema.atable WHERE col_a IS NOT NULL
UNION
SELECT col_b as value
FROM aschema.atable WHERE col_b IS NOT NULL
UNION
SELECT col_c as value
FROM aschema.atable WHERE col_c IS NOT NULL
)But I am not sure about the performance.
Actually I have rows like
id col_a col_b col_c
1 ABD CDE XYZ
2 CDE null null
3 ABD null null
3 FGH LMN nullAnd I expect as a result
ABC
ABD
CDE
FGH
LMN
XYZAny recommendation and good example?
Solution
SELECT DISTINCT value FROM (
SELECT col_a as value
FROM aschema.atable WHERE col_a IS NOT NULL
UNION
SELECT col_b as value
FROM aschema.atable WHERE col_b IS NOT NULL
UNION
SELECT col_c as value
FROM aschema.atable WHERE col_c IS NOT NULL
)The
UNION there is UNION [DISTINCT]. You don't need to wrap it in SELECT DISTINCT. Though the previous pattern may be faster if it matters if you do UNION ALL, and wrap that in one SELECT DISTINCT FROM ()SELECT col_a as value
FROM aschema.atable WHERE col_a IS NOT NULL
UNION
SELECT col_b as value
FROM aschema.atable WHERE col_b IS NOT NULL
UNION
SELECT col_c as value
FROM aschema.atable WHERE col_c IS NOT NULLWhy doesn't that work to do what you want?
If you ever find yourself writing a query like this though, I would think the problem would be in the schema itself.
Code Snippets
SELECT DISTINCT value FROM (
SELECT col_a as value
FROM aschema.atable WHERE col_a IS NOT NULL
UNION
SELECT col_b as value
FROM aschema.atable WHERE col_b IS NOT NULL
UNION
SELECT col_c as value
FROM aschema.atable WHERE col_c IS NOT NULL
)SELECT col_a as value
FROM aschema.atable WHERE col_a IS NOT NULL
UNION
SELECT col_b as value
FROM aschema.atable WHERE col_b IS NOT NULL
UNION
SELECT col_c as value
FROM aschema.atable WHERE col_c IS NOT NULLContext
StackExchange Database Administrators Q#162788, answer score: 3
Revisions (0)
No revisions yet.