0

I have a table like this

enter image description here

Using the COALESCE() function...

SELECT COALESCE(column1, column2, column3) combinedColumn from t;

I get this...

enter image description here

However, I want this....

enter image description here

I found a work around using UNION ALL but this isn't very elegant. Is there a function that works like COALESCE() except includes all values? Thanks

cget
  • 370
  • 1
  • 4
  • 21

1 Answers1

2

You can't use a coalesce here as there might be more than one value to return

UNION is the best solution (not UNION ALL because blanks)

select column1 from mytable
UNION
select column2 from mytable
UNION
select column3 from mytable

That said, if you want to maintain duplicates (if any), it's UNION ALL, or joining all the columns into a single string then splitting them out again (avoiding the UNION ALL at any cost)

JohnHC
  • 10,935
  • 1
  • 24
  • 40