如何逐月转动并仅按总金额显示前10名

时间:2017-09-05 23:33:45

标签: sql sql-server tsql top-n

我有一个动态数据集,可以从今天和一年前返回数据。 如何透过Description

来调整此数据并仅返回前10 NetWrittenPremium

我知道如何执行静态数据透视,但在这种情况下我会怎么做呢。

        ;with cte_TopClasses
    AS  ( 
            SELECT      
                        b.MonthNum,
                        b.YearNum,
                        GovClassCode + ' - ' + dda.GovClassDesc as Description,
                        ISNULL(SUM(Premium),0) as NetWrittenPremium 
            FROM        tblCalendar b 
            LEFT JOIN   ProductionReportMetrics prm ON b.YearNum = Year(prm.EffectiveDate) AND b.MonthNum=Month(prm.EffectiveDate) AND  CompanyLine = 'Arch Insurance Company'
            LEFT JOIN   [dbo].[Dynamic_Data_ArchWC] dda ON prm.QuoteGUID = dda.QuoteGuid    
            WHERE       
                        ( b.YearNum = YEAR(GETDATE())-1 and b.MonthNum >= MONTH(GETDATE())+1 ) OR 
                        ( b.YearNum = YEAR(GETDATE()) and b.MonthNum <= MONTH(GETDATE()) )      
            GROUP BY    b.YearNum ,
                        b.MonthNum, 
                        GovClassCode,   
                        dda.GovClassDesc
        )
   --here I want to pivot it
    select *
    from cte_TopClasses

目前的结果是128条记录。

enter image description here

理想的结果就是这样:

enter image description here

@Nico 它应该是这样的:

enter image description here

你们非常接近,只是提前一个月。

enter image description here

1 个答案:

答案 0 :(得分:2)

也许这有点复杂。有关动态数据透视的更多信息,您可以查看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