Sqlserver
 sql >> Datenbank >  >> RDS >> Sqlserver

SQL Server 2012:Top n basierend auf mehreren Kriterien auswählen

Sie können dies mit row_number() tun :

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;