如何将列名称指定给数据透视表

时间:2015-09-02 00:07:49

标签: sql sql-server sql-server-2008

继承我的陈述

SELECT *
FROM(
SELECT CAST([School ID] as varchar(10)) AS 'School ID', Year, Value as [Total]
FROM [sandbox].[dbo].[SchoolInformation]
WHERE Attribute = 'Total' AND Year IN ('2010','2011','2012','2013','2014')

)cte
PIVOT (SUM([Total]) FOR YEAR IN([2010],[2011],[2012],[2013],[2014]))pvt

我的问题是,有没有办法更改列名显示(year1,year2,year3,year4,year5)而不是(2010,2011,2012,2013,2014)?

谢谢:)

1 个答案:

答案 0 :(得分:1)

您可以为列添加别名,就像这样。

SELECT
    [2010] AS 'year1',
    [2011] AS 'year2',
    ....
FROM (
    SELECT ...
) ...

我能想到的另一种方法是编写动态SQL。