Search code examples
sqlplsqlunpivot

SQL case statements to change values


This is my code to do part of what I want to do:

SELECT uID, ColumnName, ColumnResult
FROM table
UNPIVOT
(
ColumnResult
for ColumnName in (COL1,COL2,COL3)
 )u

This returns something like this:

Thanks


Solution

  • You can use the following query which replaces the column name with the new value:

    select uID,
      case ColumnName 
        when 'COL1' then 'columnOne'
        when 'COL2' then 'columnTwo'
        when 'COL3' then 'columnThree'
      end ColumnName, 
      ColumnResult
    from TABLE
    unpivot
    (
      ColumnResult
      for ColumnName in (COL1,COL2,COL3)
    )u;
    

    See SQL Fiddle with Demo