对于我的生活,我无法看到我在这里做错了什么。
SELECT Series_ID
, 1 AS Jan
, 2 AS Feb
, 3 AS Mar
, 4 AS Apr
, 5 AS May
, 6 AS Jun
, 7 AS Jul
, 8 AS Aug
, 9 AS Sep
, 10 AS Oct
, 11 AS Nov
, 12 AS [Dec]
FROM
(
SELECT MONTH(ActionDate_DT) AS [MonthNum]
, Series_ID
, ID
FROM TasksSeries_V
WHERE Series_ID IN (5632751,5672397,5680669,5680670,5632713,5632752,5680902)
AND TasksSeries_V.ActionDate_DT BETWEEN '01 Jan 2011' AND '31 Dec 2011'
) AS SourceTable
PIVOT
(
COUNT(ID) FOR MonthNum IN ([1],[2],[3],[4],[5],[6],[7],[8],[9],[10],[11],[12])
) AS pvt
当我跑步时,我得到
ID Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
5632713 1 2 3 4 5 6 7 8 9 10 11 12
5632751 1 2 3 4 5 6 7 8 9 10 11 12
5632752 1 2 3 4 5 6 7 8 9 10 11 12
5672396 1 2 3 4 5 6 7 8 9 10 11 12
5672397 1 2 3 4 5 6 7 8 9 10 11 12
5680669 1 2 3 4 5 6 7 8 9 10 11 12
5680670 1 2 3 4 5 6 7 8 9 10 11 12
5680902 1 2 3 4 5 6 7 8 9 10 11 12
有什么想法吗?
答案 0 :(得分:2)
我认为问题在于,在第一个SELECT语句中,您应该使用[1]
而不是1
作为列名。
SELECT Series_ID
, [1] AS Jan
, [2] AS Feb
, [3] AS Mar
, [4] AS Apr
, [5] AS May
, [6] AS Jun
, [7] AS Jul
, [8] AS Aug
, [9] AS Sep
, [10] AS Oct
, [11] AS Nov
, [12] AS [Dec]
FROM
(
SELECT MONTH(ActionDate_DT) AS [MonthNum]
, Series_ID
, ID
FROM TasksSeries_V
WHERE Series_ID IN (5632751,5672397,5680669,5680670,5632713,5632752,5680902)
AND TasksSeries_V.ActionDate_DT BETWEEN '01 Jan 2011' AND '31 Dec 2011'
) AS SourceTable
PIVOT
(
COUNT(ID)
FOR MonthNum IN ([1],[2],[3],[4],[5],[6],[7],[8],[9],[10],[11],[12])
) AS pvt
答案 1 :(得分:0)
该行
SELECT Series_ID, 1 AS Jan, 2 AS Feb, 3 AS Mar, 4 AS Apr, 5 AS May, 6 AS Jun, 7 AS Jul, 8 AS Aug, 9 AS Sep, 10 AS Oct, 11 AS Nov, 12 AS [Dec]
表示我们应该选择文字值1和别名为Jan,文字值2和别名为Feb ...而是通过用方括号或双引号括起来引用名为1的列
SELECT Series_ID, [1] AS Jan, [2] AS Feb, [3] AS Mar, [4] AS Apr, [5] AS May, [6] AS Jun, [7] AS Jul, [8] AS Aug, [9] AS Sep, [10] AS Oct, [11] AS Nov, [12] AS [Dec]