The PIVOT operator can be used to transform a set of rows into columns while the UNPIVOT operator complements the PIVOT operator by allowing you to turn columns into rows.
The following sample will use AdventureWorks database.
Sample Code
USE AdventureWorks GOSELECT DaysToManufacture, sum(StandardCost) AS Cost FROM Production.ProductGROUP BY DaysToManufactureHere is the result DaysToManufacture Cost----------------- ---------------------0 1251.78511 34477.52662 2513.75784 92092.823SELECT 'Cost' AS Cost, [0], [1], [2], [3], [4]FROM(SELECT DaysToManufacture, StandardCost FROM Production.Product) AS SourcePIVOT(sum(StandardCost)FOR DaysToManufacture IN ([0], [1], [2], [3], [4])) AS PHere is the result Cost 0 1 2 3 4
---- --------------------- --------------------- --------------------- --------------------- ---------------------
Cost 1251.7851 34477.5266 2513.7578 NULL 92092.823