2

I have read solutions to merge two 3 row columns into one 6 row column but I think I want something else.

Let's say you have two columns:

Column 1      Column 2
NULL          D
B             NULL
C             NULL

Is there a way to get one table that has all the information?

Column 3
D
B
C

I'm getting these columns after doing UNION ALL. So these are not the database tables but the columns that I have selected.

I'm using postgresql.

This is what I tried so far:

SELECT col1, col2, t2.col3 from t1
LEFT JOIN t2
ON t1.col1 like  '%.'||t2.col3

UNION ALL

SELECT col1, col2, col3 From t1
LEFT JOIN t2
ON t1.col1 = concat(t2.col3, '.')

You can see that as I'm joining the same two tables on different conditions, I am getting different pieces of information.

James Z
  • 12,209
  • 10
  • 24
  • 44
  • Could you use code tags please? It gets confusing telling which Columns your talking about, is that a `D` column or a `DB` column? `Column 3 D` `B C` or `Column 3 D B C` ? – Ejesalva Jul 28 '15 at 14:18
  • 1
    possible duplicate of [What is the PostgreSQL equivalent for ISNULL()](http://stackoverflow.com/questions/2214525/what-is-the-postgresql-equivalent-for-isnull) – Jamiec Jul 28 '15 at 14:19

1 Answers1

3

The function you're looking for is COALESCE

SELECT COALESCE(Column1,Column2) AS Column3 
FROM t1

The way you were trying to do it was super-complicated, you could have probably also solved it like

SELECT Column1 FROM t1 WHERE Column1 IS NOT NULL
UNION ALL
SELECT Column2 FROM t1 WHERE Column2 IS NOT NULL

I'd stick with the first way

Jamiec
  • 133,658
  • 13
  • 134
  • 193