I'm a beginner to Postgres and need to do something like this. My Postgres query output has 2 columns. I need to combine these two like below.
Column 1:
A
B
C
Column 2:
D
D
D
Output Column:
A
B
C
D
(all values from column 1 and distinct values from column 2)
Is this possible in PostgreSQL?
Select the same number of columns for each query. Corresponding columns must be the same general data type. Corresponding columns must all either allow null values or not allow null values. If you want to order the columns, specify a column number because the names of the columns you are merging are probably different.
(each row of #t is presented 3 times, for а=1 and а=2 and а=3). For the first case we take value from Column1, and for the second - from Column2 and for the Third - from Column3. Here, certainly, there is both union and join but, in my opinion, the title's question means single scanning the table.
Now you know how to select single columns. In the real world, you will often want to select multiple columns. Luckily, SQL makes this really easy. To select multiple columns from a table, simply separate the column names with commas!
Hi there is one fancy way:
select distinct unnest(array[
column1
, column2
])
from table
You need something like this :
select col from (
select Column1 as col from <your table >
union all
select distinct Column2 as col from <your table>
) as myview order by col
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With