我需要PIVOT中的行总列
MS SQL查询如下,但总列显示NULL值。
请求帮助。
谢谢
SELECT *
FROM ( SELECT DATENAME(yyyy, Request_Date) AS Year ,
DATENAME(mm, Request_Date) AS Month ,
Project_Name ,
Status ,
Request_Type ,
COUNT(Test_Name) AS Test
FROM request_details2
GROUP BY DATENAME(yyyy, Request_Date) ,
DATENAME(mm, Request_Date) ,
Request_Type ,
Status ,
Project_Name
HAVING ( Request_Type = 'Test Request' )
) AS s PIVOT
( SUM(Test) FOR [Month] IN ( January, february, March, April, May, June, July,
August, September, October, November, December,
Total ) )
AS piv
答案 0 :(得分:0)
以下查询将用于所需目的:)
SELECT
year, Project_Name,Status , Request_Type ,
January = SUM(January),
february = SUM(february),
March = SUM(March),
April = SUM(April),
May = SUM(May),
June = SUM(June),
July = SUM(July),
August = SUM(August),
September = SUM(September),
October = SUM(October),
October = SUM(November),
December = SUM(December),
Total = SUM(ISNULL(January,0) + ISNULL(february,0) + ISNULL(March,0) + ISNULL(April,0) + ISNULL(May,0) +
ISNULL(June,0) +ISNULL(July,0) + ISNULL(August,0) + ISNULL(September,0) + ISNULL(October,0) + ISNULL(November,0) + ISNULL(December,0) )
FROM (
SELECT *
FROM ( SELECT DATENAME(yyyy, Request_Date) AS Year ,
DATENAME(mm, Request_Date) AS Month ,
Project_Name ,
Status ,
Request_Type ,
COUNT(Test_Name) AS Test
FROM request_details2
GROUP BY DATENAME(yyyy, Request_Date) ,
DATENAME(mm, Request_Date) ,
Request_Type ,
Status ,
Project_Name
HAVING ( Request_Type = 'Test Request' )
) AS s PIVOT
( SUM(Test) FOR [Month] IN ( January, february, March, April, May, June, July, August, September, October, November, December ))
AS piv
) as b
group by
year, Project_Name,Status , Request_Type