sqlserver 分组取前几行

select top 100  ss1.* from(select rank() OVER (PARTITION BY ss.senderip,ss.report  order by ss.num desc ) AS 'RowNumber', ss.* from (select  DATEPART(hh,reporttime) as report,senderip,count(number) as num   from JdEdm_SendReport with (nolock) 
where status=1 and domain='qq.com' and reportTime >='2012-08-15' group by reporttime,senderip  ) as ss

 )  as ss1 where RowNumber<5 order by ss1.senderip,ss1.report,ss1.num desc



主要函数:rownum(),rank,dense rank() ...

你可能感兴趣的:(sqlserver)