SQL Server 分组排序后取第N条数据(或前N条)

SQL Server 分组排序后取第N条数据(或前N条)

记录一个分组取前N条数据的SQL:

1 SELECT  *
2 FROM    ( SELECT    ROW_NUMBER() OVER ( PARTITION  BY t1.X ORDER BY t1.Y ) AS RNUM ,
3                     *
4           FROM      Table1 t1
5         ) AS T
6 WHERE   T.RNUM = N

X:分组的字段;

Y:排序的字段;

N:第N条

 

posted @ 2019-06-15 08:25  grj001  阅读(320)  评论(0编辑  收藏  举报