How to PIVOT this query and display only TOP 10 records filtered by SUM(NetWrittenPremium) DESC

ぐ巨炮叔叔 提交于 2019-12-25 08:09:10

问题


In this query I cant understand what would be the proper syntax to PIVOT it by month and also display just top 10 records based on SUM(NetWrittenPremium).

;with cte_TopClasses
AS  ( 
select  
        b.YearNum,
        b.MonthNum,
        REPLACE(ClassCode,'+','') + ' - '+ QLL.Description as Description,
        SUM( Premium) as NetWrittenPremium
FROM        tblCalendar b  
LEFT JOIN   ProductionReportMetrics prm ON b.MonthNum=Month(prm.EffectiveDate) AND b.YearNum = YEAR(EffectiveDate)  
AND prm.EffectiveDate >=DateAdd(yy, -1, DATEADD(d, 1, EOMONTH(GETDATE()))) AND prm.EffectiveDate <= EOMONTH(GETDATE())  AND CompanyLine = 'Ironshore Insurance Company' 
LEFT JOIN  NetRate_Quote_Insur_Quote Q ON prm.NetRate_QuoteID = Q.QuoteID
LEFT JOIN NetRate_Quote_Insur_Quote_Locat QL ON Q.QuoteID = QL.QuoteID  

LEFT JOIN   (SELECT * FROM NetRate_Quote_Insur_Quote_Locat_Liabi nqI 
            JOIN ( SELECT LocationID as LocID, MAX(ClassCode) as ClCode 
            FROM NetRate_Quote_Insur_Quote_Locat_Liabi  GROUP BY LocationID ) nqA 
            ON nqA.LocID = nqI.LocationID AND nqA.ClCode = nqI.ClassCode ) QLL 
            ON QLL.LocationID = QL.LocationID 

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,ClassCode,        QLL.Description
    )
SELECT 
        --TOP 10
        RANK() OVER (ORDER BY NetWrittenPremium DESC) AS Rank, * 
FROM    cte_TopClasses
WHERE   Description IS NOT NULL
ORDER BY NetWrittenPremium DESC,YearNum,MonthNum

The result should look something like that:

If I use the query below and then using matrics in SSRS to PIVOT it - then after grouping by Description it only displays me 2 Description.

    ;with cte_TopClasses
AS  ( 
select  
        b.YearNum,
        b.MonthNum,
        REPLACE(ClassCode,'+','') + ' - '+ QLL.Description as Description,
        SUM( Premium) as NetWrittenPremium
FROM        tblCalendar b  
LEFT JOIN   ProductionReportMetrics prm ON b.MonthNum=Month(prm.EffectiveDate) AND b.YearNum = YEAR(EffectiveDate)  
AND prm.EffectiveDate >=DateAdd(yy, -1, DATEADD(d, 1, EOMONTH(GETDATE()))) AND prm.EffectiveDate <= EOMONTH(GETDATE())  AND CompanyLine = 'Ironshore Insurance Company' 
LEFT JOIN  NetRate_Quote_Insur_Quote Q ON prm.NetRate_QuoteID = Q.QuoteID
LEFT JOIN NetRate_Quote_Insur_Quote_Locat QL ON Q.QuoteID = QL.QuoteID  

LEFT JOIN   (SELECT * FROM NetRate_Quote_Insur_Quote_Locat_Liabi nqI 
            JOIN ( SELECT LocationID as LocID, MAX(ClassCode) as ClCode 
            FROM NetRate_Quote_Insur_Quote_Locat_Liabi  GROUP BY LocationID ) nqA 
            ON nqA.LocID = nqI.LocationID AND nqA.ClCode = nqI.ClassCode ) QLL 
            ON QLL.LocationID = QL.LocationID 

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,ClassCode,        QLL.Description
    )
SELECT *
    FROM (SELECT RANK() OVER (ORDER BY NetWrittenPremium DESC) AS Rank, * 
    FROM    cte_TopClasses
    WHERE   Description IS NOT NULL) AA
WHERE AA.Rank <= 10
ORDER BY AA.NetWrittenPremium DESC, AA.YearNum, AA.MonthNum

And the result of it in SSRS matrics :


回答1:


You could try something like this at the end of the query, rather than what is there now:

SELECT *
    FROM (SELECT RANK() OVER (ORDER BY [Description] DESC) AS Rank, * 
    FROM    cte_TopClasses
    WHERE   Description IN (SELECT [Description]
FROM (SELECT RANK() OVER (ORDER BY SUM(NetWrittenPremium) DESC) AS [Rank], [Description], SUM(NetWrittenPremium) AS total
        FROM cte_TopClasses
        WHERE [Description] IS NOT NULL
        GROUP BY [Description]) BB
    WHERE [Rank] <= 10)) AA
ORDER BY YearNum, MonthNum

This wraps the query in a SELECT, and filters the ranked results to the 10 you want.

Then use a matrix in the report to pivot the results.



来源:https://stackoverflow.com/questions/39209657/how-to-pivot-this-query-and-display-only-top-10-records-filtered-by-sumnetwritt

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!