Sqlserver
 sql >> Database >  >> RDS >> Sqlserver

SQL Server 2012:selezionare Primi n in base a più criteri

Puoi farlo 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;