Sqlserver
 sql >> Base de Dados >  >> RDS >> Sqlserver

SQL Server 2012:selecione Top n com base em vários critérios


Você pode fazer isso com row_number() :
select region, month, MemberId, sales
from (select region, month, MemberId, sum(sales) as sales
             row_number() over (partition by region, month order by sum(sales) desc) as seqnum
      from table t
      group by region, month, MemberId
     ) t
where seqnum <= 2;