慕沐林林
SELECT <non-pivoted column>, [first pivoted column] AS <column name>, [second pivoted column] AS <column name>, ... [last pivoted column] AS <column name>FROM (<SELECT query that produces the data>) AS <alias for the source query>PIVOT( <aggregation function>(<column being aggregated>)FOR[<column that contains the values that will become column headers>] IN ( [first pivoted column], [second pivoted column], ... [last pivoted column])) AS <alias for the pivot table><optional ORDER BY clause>;USE AdventureWorks2008R2 ;GOSELECT DaysToManufacture, AVG(StandardCost) AS AverageCost FROM Production.ProductGROUP BY DaysToManufacture; DaysToManufacture AverageCost0 5.08851 223.882 359.10824 949.4105 -- Pivot table with one row and five columnsSELECT 'AverageCost' AS Cost_Sorted_By_Production_Days, [0], [1], [2], [3], [4]FROM(SELECT DaysToManufacture, StandardCost FROM Production.Product) AS SourceTablePIVOT(AVG(StandardCost)FOR DaysToManufacture IN ([0], [1], [2], [3], [4])) AS PivotTable;Here is the result set.Cost_Sorted_By_Production_Days 0 1 2 3 4 AverageCost 5.0885 223.88 359.1082 NULL 949.4105