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条

 

    原文作者:sql
    原文地址: https://www.cnblogs.com/forever-star/p/5483199.html
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞