Search code examples
sqloracle10grdbms

Select query with names columns


I want have a query on the table values

Column1 Column2 Column3   
-----------------------
a         b      c    
d         e      f

Result should be

Column1 a
Column2 b
Column3 c
Column1 d
Column2 e
Column3 f

Basically a key value pairs. Is it possible in Oracle?

Thanks for the help in advance


Solution

  • You can do it with UNION ALL, like this:

    SELECT 'Column1' as Name, Column1 as Value FROM my_table
    UNION ALL
    SELECT 'Column2' as Name, Column2 as Value FROM my_table
    UNION ALL
    SELECT 'Column3' as Name, Column3 as Value FROM my_table
    

    Here is a demo on sqlfiddle.