Maybe it's somewhat complicated. More information about dynamic pivot, you can check this post.
--begin get the ordered month name of past one year
DECLARE @cols AS NVARCHAR(MAX)
DECLARE @startMonth AS INT
--line 1
SET @startMonth=MONTH(GETDATE())+1
WHILE(@startMonth<=12)
BEGIN
SET @cols= COALESCE(@cols,'')+'['+CAST(@startMonth AS varchar)+'],';
SET @startMonth=@startMonth+1;
END
--line 2
SET @startMonth=MONTH(GETDATE())+1
DECLARE @countFlag INT
SET @countFlag=0
WHILE(@startMonth>1)
BEGIN
SET @countFlag=@countFlag+1;
SET @cols= COALESCE(@cols,'')+'['+CAST(@countFlag AS varchar)+'],';
SET @startMonth=@startMonth-1;
END
SET @cols=SUBSTRING(@cols,1,LEN(@cols)-1)
-- end
DECLARE @query NVARCHAR(MAX)
SET @cols=REPLACE(@cols,'10','October');
SET @cols=REPLACE(@cols,'11','November');
SET @cols=REPLACE(@cols,'12','December');
SET @cols=REPLACE(@cols,'1','January');
SET @cols=REPLACE(@cols,'2','February');
SET @cols=REPLACE(@cols,'3','March');
SET @cols=REPLACE(@cols,'4','April');
SET @cols=REPLACE(@cols,'5','May');
SET @cols=REPLACE(@cols,'6','June');
SET @cols=REPLACE(@cols,'7','July');
SET @cols=REPLACE(@cols,'8','August');
SET @cols=REPLACE(@cols,'9','September');
SET @query = '
SELECT
TOP 10
*,
ISNULL([October],0)+ISNULL([November],0)+ISNULL([December],0)+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) AS Total
FROM
(
SELECT DateName(month, DateAdd(month, MonthNum, -1)) AS [MonthName],[Description],SUM(NetWrittenPremium) AS SubTotal FROM dbo.cte_TopClasses WHERE ((YearNum-YEAR(GETDATE()))*12+MonthNum-MONTH(GETDATE()))>-12 AND ((YearNum-YEAR(GETDATE()))*12+MonthNum-MONTH(GETDATE()))<=0 GROUP BY [MonthNum],[Description]
) AS source
PIVOT
(
SUM(SubTotal)
FOR MonthName
IN (' + @cols + ')
) AS pvtMonth
ORDER BY Total
'
EXEC SP_EXECUTESQL @query
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…