Home > Database > Mysql Tutorial > SQLServer 获得用户最新或前n条订单的几种SQL语句小结

SQLServer 获得用户最新或前n条订单的几种SQL语句小结

WBOY
Release: 2016-06-07 18:02:14
Original
1146 people have browsed it

场景:有一张用户表,一个订单表,要求获得一个用户对应的最新的一条订单信息。

实现以上要求,我们可以用以下几种方式,但是效率却相差很远。
首先我们在Order表中,创建一个索引:

CREATE UNIQUE INDEX idx_eid_odD_oidD ON Orders(EmployeeID,OrderDate DESC,OrderID DESC)
  多个OrderId是为了在OrderData相同的情况下,按订单号倒序,是个辅助属性。
方法1:  
代码如下:
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
)

如果想获得前n条订单信息,把 = 号改成IN,然后TOP(n)就可以了。
不论是取一条还是多条,即使有索引,数据多的情况下,也是最慢的。

方法2:
代码如下:
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

方法2只能取一条信息,不能取多条信息。

在取一条的情况下,这个要比方法1快多了,因为用户相比订单信息要少很多。

方法3:
代码如下:
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

这个应用到了SQL Server 2005或更高版本的一些新特性,这个效率要比方法2还好。
如果想取得多条,只需更改TOP(n)即可。

APPLY详解,参见
方法4:
代码如下:
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

这个ROW_NUMBER函数也是在SQL Server 2005后新增的,使用这个和方法3查不多,甚至比3更好,但要注意一点是先按EmployeeID分区,然后再排序。
结合以上方法, 建议用方法3。
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template