sql >> Base de Datos >  >> RDS >> Sqlserver

SQL Server 2012:seleccione Top n en función de varios criterios

Puedes hacer esto con 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;