复制代码 代码如下:
SELECT EmployeeID,OrderID FROM Orders AS O1
WHERE OrderID = (
SELECT TOP(1)OrderID FROM Orders AS O2
WHERE O1.EmployeeID = O2.EmployeeID
ORDER BY OrderDate DESC ,OrderID DESC
)
复制代码 代码如下:
SELECT O.EmployeeID,O.OrderID FROM (
SELECT EmployeeID,(SELECT TOP(1)OrderID FROM Orders AS O2 WHERE E.EmployeeID = O2.EmployeeID ORDER BY OrderDate DESC,OrderID DESC) AS OrderID
FROM Employees AS E
) AS EO
INNER JOIN Orders AS O
ON EO.OrderID = O.OrderID
复制代码 代码如下:
SELECT E.EmployeeID,O.OrderID FROM Employees AS E
CROSS APPLY (
SELECT TOP(1)* FROM Orders AS O1 WHERE E.EmployeeID = O1.EmployeeID ORDER BY O1.OrderDate DESC,O1.OrderID DESC
) AS O
复制代码 代码如下:
SELECT O1.EmployeeID,O1.OrderID
FROM Orders O1 JOIN (
SELECT ROW_NUMBER() OVER(PARTITION BY EmployeeID ORDER BY OrderDate DESC,OrderID DESC) AS RowNumber,*
FROM Orders AS OT
) AS O2
ON O1.OrderID = O2.OrderID
WHERE O2.RowNumber = 1
新闻热点
疑难解答