使用PIVOT()将以下查询转换为透视查询。
SELECT CategoryID, AVG(UnitPrice)
FROM Products
GROUP BY CategoryID;
您的结果集应如下所示:
Per Category 1 2 3 4 5 6 7 8
Average Unit Price 37.9791 23.0625 25.1600 28.7300 20.2500 54.0066 32.3700 20.6825
这是edx最终评估的一部分,我已经获得及格分数,但想弄清楚我无法解决的问题
我的代码如下:
SELECT 'CategoryID' AS [Per Category], [1], [2], [3], [4], [5], [6], [7], [8]
FROM (
SELECT UnitPrice, CategoryID FROM Products
) AS src
PIVOT (AVG(UnitPrice)
FOR CategoryID IN ([1], [2], [3], [4], [5], [6], [7], [8])
) AS pvt
我得到的结果是:
Per Category 1 2 3 4 5 6 7 8
CategoryID 37.9791 23.0625 25.1600 28.7300 20.2500 54.0066 32.3700 20.6825
我需要帮助更改第二行的名称以使其看起来像预期的结果:
Per Category 1 2 3 4 5 6 7 8
Average Unit Price 37.9791 23.0625 25.1600 28.7300 20.2500 54.0066 32.3700 20.6825
到目前为止,我找不到任何说明如何为该行获取两个自定义名称的教程