Oracle SQL -- select from two columns and combine into one
Here is solution without using unpivot.
with columns as ( select level as colNum from dual connect by level <= 2),results as ( select case colNum when 1 then Val1 when 2 then Val2 end Val, score from vals, columns)select * from results where val is not null
Here is essentially the same query without the WITH clause:
select case colNum when 1 then Val1 when 2 then Val2 end Val, score from vals, (select level as colNum from dual connect by level <= 2) columns where case colNum when 1 then Val1 when 2 then Val2 end is not null
Or a bit more concisely
select * from ( select case colNum when 1 then Val1 when 2 then Val2 end Val, score from vals, (select level as colNum from dual connect by level <= 2) columns ) results where val is not null
If you're on Oracle 11, unPivot will help:
SELECT * FROM valsUNPIVOT ( val FOR origin IN (val1, val2) )
you can choose any names instead of 'val' and 'origin'.