------解决方案-------------------- select * from (select *,rank() over (partition by [分组的字段] order by [根据谁排序的字段] desc/asc)rn form [表名])temp where rn <N
------解决方案-------------------- 利用开窗函数,一条语句就可以了,思路如下: 1.利用ROW_NUMBER()OVER(partition BY [分组条件1]...[,分组条件n] ORDER BY [排序条件1]...[,排序条件n]) 2.过滤出ROW_NUMBER小于等于N的数据 3.Sql这样大致: SELECT * FROM ( SELECT SN = ROW_NUMBER()......(这里按照上面给出的公式) ,* FROM [表] ) tmp WHERE tmp.SN <=@MaxNumber (@MaxNumber就是你要的N)
------解决方案--------------------