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、采用游标方法。
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、采用游标方法。