SQL Server中Group分组获取Top N方法实现

1、采用row_number方法。

SELECT *
FROM (SELECT RowNumber =row_number() OVER (PARTITION BY TaskID ORDER BY ProduceDate DESC), * FROM dbo.ShippingDocument)t
WHERE t.RowNumber< 2 ORDER BY t.TaskID ASC

2、采用游标方法。

posted @ 2015-09-01 10:11  [在路上ing]  阅读(325)  评论(0编辑  收藏  举报