如何获得每个描述的总和并在动态数据透视表中对其进行排序

How to get a total sum for each Description and order it in dynamic pivot

我需要根据每个 Class 的总数 sum 只显示前 10 行,并按总和 desc 排序。 这就是我想要实现的目标:

所以我有我的动态数据透视表,但我无法理解如何为每一行获取 total 列,然后仅显示 Total 列的前 10 个。

DECLARE @cols AS NVARCHAR(MAX),
        @query  AS NVARCHAR(MAX)

SELECT @cols = STUFF(
                    (SELECT distinct ',' + QUOTENAME([YearMonth]) 
                    FROM #MyTable
                    FOR XML PATH(''), TYPE).value('.', 'NVARCHAR(MAX)'),1,1,'')
--print @cols

SELECT @query = 
'
        ;with cte_TopClasses
AS  ( 
        SELECT      
                    CAST(b.MonthNum as varchar(2)) + ''/''+ CAST(b.YearNum as varchar(4)) as YearMonth,
                    b.YearNum,
                    b.MonthNum,
                    GovClassCode + ''- '' + dda.GovClassDesc as Class,
                    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()) )  
                    AND GovClassCode + '' - '' + dda.GovClassDesc IS NOT NULL   
        GROUP BY    b.YearNum ,
                    b.MonthNum, 
                    GovClassCode,   
                    dda.GovClassDesc
    )
SELECT * FROM
(SELECT     
    [YearMonth], 
    Class,
    NetWrittenPremium
FROM cte_TopClasses)X 

PIVOT 
(
    sum(NetWrittenPremium)
    for [YearMonth] in (' + @cols + ')
) P
'

EXEC SP_EXECUTESQL @query

输出为 73 行:

更新:使用 GROUPING SETS

更新:这是 PIVOT 之前数据的样子:

这个怎么样:

DECLARE @cols AS NVARCHAR(MAX),
        @query  AS NVARCHAR(MAX)

SELECT @cols = STUFF(
                    (SELECT distinct ',' + QUOTENAME([YearMonth]) 
                    FROM #MyTable
                    FOR XML PATH(''), TYPE).value('.', 'NVARCHAR(MAX)'),1,1,'') + ',' + '[Total]'
--print @cols

SELECT @query = 
'
        ;with cte_TopClasses
AS  ( 
        SELECT      
                    ISNULL(CAST(b.MonthNum as varchar(2)) + ''/''+ CAST(b.YearNum as varchar(4)), ''Total'') as YearMonth,
                    b.YearNum,
                    b.MonthNum,
                    GovClassCode + ''- '' + dda.GovClassDesc as Class,
                    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()) )  
                    AND GovClassCode + '' - '' + dda.GovClassDesc IS NOT NULL   
        GROUP BY    GROUPING SETS
        (
                (
                    b.YearNum ,
                    b.MonthNum, 
                    GovClassCode,   
                    dda.GovClassDesc
                )
                , 
                (
                    GovClassCode
                   ,dda.GovClassDesc
                )
        )
    )
SELECT * FROM
(SELECT     
    [YearMonth], 
    Class,
    NetWrittenPremium
FROM cte_TopClasses)X 

PIVOT 
(
    sum(NetWrittenPremium)
    for [YearMonth] in (' + @cols + ')
) P
'

EXEC SP_EXECUTESQL @query

想法是使用 GROUPING SETS

在 T-SQL 中添加 Total