I need to display only top 10 Class
based on Total
(SUM(Premium))
column.
I go to group ClassCode properties --> Filters and setting top 10 by SUM(NetWrittenPremium) but it doesnt work.
I need to display only top 10 and also the total amount should be for only those 10.
Cant understand how to achieve it.
here is my query:
;WITH cte_TopClasses
AS (
SELECT
c.YearNum,
c.MonthNum,
DD.ClassCode,
ISNULL(SUM(prm.Premium),0) as NetWrittenPremium
FROM tblCalendar c
LEFT JOIN ProductionReportMetrics prm ON c.YearNum = YEAR(prm.EffectiveDate) and c.MonthNum = MONTH(prm.EffectiveDate)
AND CompanyGUID = '18E04C99-D796-4CFA-B1E7-28328321C8AD'
LEFT JOIN [dbo].[Dynamic_Data_GLUnitedSpecialty] DD on prm.QuoteGUID = DD.QuoteGuid
WHERE ( c.YearNum = YEAR(GETDATE())-1 and c.MonthNum >= MONTH(GETDATE())+1 ) OR
( c.YearNum = YEAR(GETDATE()) and c.MonthNum <= MONTH(GETDATE()) )
GROUP BY c.YearNum,
c.MonthNum,
DD.ClassCode--,prm.Premium
)
SELECT ROW_NUMBER() OVER (PARTITION BY ClassCode ORDER BY NetWrittenPremium DESC),*
FROM cte_TopClasses
and my outcome from the query:
@Alan Thanks. Query output look like that:
If I add ClassCode in order by
in dense_rank
then $142,000 is not gonna be in a query. Which is not good.
Any other ideas? Maybe I can use partition
function?
In Group Properties add a sort and set
Sort by expression "=Sum(NetWrittenPremium)"
Order = "Z to A"
Them in the filter as follows:
Expression = "=Sum(NetWrittenPremium)"
Operator = Top N
Value = 10
Actually I've just noticed the Total row.... This will not calculate the total correctly and you cannot use aggregations in filters on the tablix (which would have worked otherwise).
You best bet would be to push this back to the server and do it there.
I can't test this on your data directly but it should work, I tested on someting similar...
SELECT r.* FROM
(SELECT d.*
, dense_rank() OVER(ORDER BY TotalNWP Desc) AS rnk
FROM
(SELECT DISTINCT
ClassCode, YearNum, MonthNum
, SUM(t.NetWrittenPremium) OVER (PARTITION BY ClassCode, YearNum, MonthNum) AS NetWrittenPremium
, SUM(t.NetWrittenPremium) OVER (PARTITION BY ClassCode) AS TotalNWP
FROM cte_TopClasses t
) d
) r
WHERE rnk <=10
Now there should be no need to do any filtering in SSRS, just sorting by the rnk
column.
You only remaining problem is how to determine which of the last results (which all have the same total) take precedent over the others. You could do something like adding ClassCode to the dense_rank function to the are chosen alphabetically but that;s for yo to decide I guess.
来源:https://stackoverflow.com/questions/46085189/how-to-display-top-10-based-on-total-amount-in-ssrs-2012