Hi Everyone,
Here's a sample Query for a simple pivot I got from technet microsoft:
SELECT 'AverageCost' AS Cost_Sorted_By_Production_Days,
[0], [1], [2], [3], [4]
FROM
(SELECT DaysToManufacture, StandardCost
FROM Production.Product) AS SourceTable
PIVOT
(
AVG(StandardCost)
FOR DaysToManufacture IN ([0], [1], [2], [3], [4])
) AS PivotTable;
As you can see they used ([0],[1],[2],[3],[4]) as pivoted columns.
Is there a way I can change that pivoted columns to a SELECT Query like this:
SELECT 'AverageCost' AS Cost_Sorted_By_Production_Days,
[0], [1], [2], [3], [4]
FROM
(SELECT DaysToManufacture, StandardCost
FROM Production.Product) AS SourceTable
PIVOT
(
AVG(StandardCost)
FOR DaysToManufacture IN (SELECT DayCount FROM Production.Days)
) AS PivotTable;
I cannot encode the pivoted columns manually because it is always changing.
Please help!
TIA!