select row_number() over (order by qty desc) as rank,s1.stor_id,s1.qty from ( select stor_id, qty=sum(qty) from sales ...
support.microsoft.com