Home > Blockchain >  Postgres - Combine two columns with distinct values and get distinct on the combined values
Postgres - Combine two columns with distinct values and get distinct on the combined values

Time:01-29

I have a table called test_table that looks like below

Column Type Collation Nullable Default
ts timestamp with time zone not null
ba integer
ca integer

Now if I run distinct on column ba, I get

ba
5
10
11

Similarly if I run distinct on column ca, I get

ca
5
10
18
20

Now I want to combine the values of these two distinct columns and get a distinct out of the combined values such that the output looks like below

Expected Output

combo
5
10
11
18
20

I can do a DISTINCT on multiple columns like so

select distinct ba, ca from test_table;

But I can't figure out how do I combine these two columns and get distinct values out of it.

CodePudding user response:

It sounds like you are after a union of these two columns:

select ba
from test_table
union
select ca
from test_table;
  • Related