How Do I Use PIVOT On This Data:?

前端 未结 4 1306
抹茶落季
抹茶落季 2021-01-16 16:24

I have a SQL Server table that looks like this:

RESOURCE |  DESCRIPTION | VALUE
Test A      Name        | Resource A-xyz
Test A   |  Height      | 20
Test A          


        
4条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-01-16 16:46

    The resulting column list is based on the ordering you provide within the PIVOT:

    SELECT *
    FROM Table1
    PIVOT(MAX(VALUE) FOR DESCRIPTION IN (Name,Height,Unit,Location,Volume,Width))p
    

    Demo: SQL Fiddle

    If you have changing values for DESCRIPTION it's worthwhile to build this query dynamically, there are plenty of good examples for 'dynamic pivot' to be found.

提交回复
热议问题