Blog Pages

PIVOT

PIVOT rotates a table-valued expression by turning the unique values from one column in the expression into multiple columns in the output.


SELECT 'AverageCost' AS Cost_Sorted_By_Production_Days, 
[0], [1], [2], [3], [4]
FROM
(SELECT DaysToManufacture, StandardCost 
    FROM Production.Product) SourceTable
PIVOT
(
AVG(StandardCost)
FOR DaysToManufacture IN ([0], [1], [2], [3], [4])
) PivotTable

Notes:
  • Available from SQL Server 2005.
  • When aggregate functions are used with PIVOT, the presence of any null values in the value column are not considered when computing an aggregation.
  • Pivot columns are specific values from the column of the source table, and can't be declared dynamic.

No comments:

Post a Comment