SKumar
SKumar

Reputation: 59

Trying to pivot output of SQL query

I need to swap rows with columns and vice versa. Below is the query:

SELECT TypeName, value
FROM [RamCustomData]
where Ram = 1024 and TypeName in ('RamType','productionUnitID','version')
order by 2

Raw output:

TypeName            Value
RamType             XYZ
productionUnitID    ABC
version             V123

Expected output

TypeName     RamType        ProductionUnitID      Version
Value         XYZ             ABC                  V123

Could you please advise me on how to approach this task?

Upvotes: 0

Views: 43

Answers (1)

John Cappelletti
John Cappelletti

Reputation: 81990

This is a standard PIVOT... We just had to include TypeName='Value'

Depending on your data, the ...and TypeName in ('RamType','productionUnitID','version') is optional

Example

Select TypeName='Value'
      ,*
 From (
         Select TypeName
               ,value
          From  [RamCustomData]
          Where Ram = 1024 
            and TypeName in ('RamType','productionUnitID','version')
      ) src
 Pivot (max(Value) for TypeName in ([RamType],[productionUnitID],[version]) ) pvt

Returns

TypeName    RamType productionUnitID    version
Value       XYZ     ABC                 V123

Upvotes: 1

Related Questions