WITH ranking AS( SELECT name,SUM(revenue) AS total_revenue,State, RANK() over (PARTITION by year ORDER BY SUM(revenue) desc) AS revenue_rank,year FROM (SELECT a.name,c.State,ii.