I want to find out the top 3 spending from each quarter. My current code is printing all the spending from each quarter but i want to show only the top 3 from each quarter.
SELECT SUM(f.Spend) AS Total_Spend, e.expenset_id,e.[Expense Type],
ROW_NUMBER() OVER(PARTITION BY e.expenset_id ORDER BY SUM(f.Spend) DESC) Rank,
CASE
WHEN DATEPART(mm,f.Date) IN (4,5,6) THEN 'Q2'
WHEN DATEPART(mm,f.Date) IN (7,8,9) THEN 'Q3'
WHEN DATEPART(mm,f.Date) IN (10,11,12) THEN 'Q4'
ELSE 'Q1'
END AS Quarter
FROM fact_tbl f
JOIN expense_type e ON f.expenset_id=e.expenset_id
GROUP BY e.[Expense Type],e.expenset_id,DATEPART(mm,f.Date)
ORDER BY SUM(f.Spend) DESC