1USE Northwind
2GO
3
4SELECT o1.OrderID, o1.OrderDate, ca.OrderID AS NextOrder,
5 ca.OrderDate AS NextOrderDate, CustomerID,
6 DATEDIFF(DAY, o1.OrderDate,ca.OrderDate) DaysToNextOrder
7FROM Orders AS o1
8 CROSS APPLY
9 (SELECT TOP 1 o.OrderDate, o.OrderID
10 FROM Orders AS o
11 WHERE o.customerID = o1.customerID
12 AND o.OrderID > o1.OrderID
13 ORDER BY OrderID) AS ca
14
15ORDER BY CustomerID, o1.OrderID
16