Search code examples
sqloracle-databaseoracle12c

Transpose columns to rows with column header in Oracle


Have tried to convert the columns to rows in oracle but the column headers should be the first column values.

Note: The select statement with WHERE clause. Have tried PIVOT and UNPIVOT but unfortunately I am not getting the desired output.

enter image description here

It should be converted to

enter image description here

Please assist.


Solution

  • Unpivot, as you said.

    Sample data:

    SQL> with test (column1, column2, column3) as
      2    (select 385, 0, 29 from dual)
    

    Query:

      3  select *
      4  from test
      5  unpivot (value for type in (column1, column2, column3));
    
    TYPE         VALUE
    ------- ----------
    COLUMN1        385
    COLUMN2          0
    COLUMN3         29
    
    SQL>