SQL取分组Top n条记录(转)

use Northwind

Select *
FROM orders A
Where EXISTS
          (Select COUNT(1)
         FROM orders
         Where employeeid = A.employeeid AND orderid > A.orderid
         HAVING COUNT(1) < 3)
ORDER BY EmployeeID

语义:在库Northwind中查询表Orders里按EmployeeID分组,每个EmployeeID取前三条OrderID最大的记录

转载于:https://www.cnblogs.com/n666/archive/2010/02/22/2190952.html

你可能感兴趣的:(SQL取分组Top n条记录(转))