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?