這裏是我的建議:
Use Northwind
GO
select ords.OrderID , ords.OrderDate , '<-->' as Sep1 , derived1.* from
dbo.Orders ords
join
(
select CustomerID, OrderID, ROW_NUMBER() OVER(PARTITION BY CustomerID ORDER BY OrderId DESC) AS ThisCustomerCardinalOrderNumber from dbo.Orders
) as derived1
on ords.OrderID = derived1.OrderID
where
derived1.ThisCustomerCardinalOrderNumber = 3
and ords.OrderDate between '06/01/1997' and '07/01/1997'
編輯:::::::::
我把我的CTE例子,並重新整理爲多個客戶(如下圖所示)。 給它大學嘗試。
Use Northwind
GO
declare @BeginDate datetime
declare @EndDate datetime
select @BeginDate = '01/01/1900'
select @EndDate = '12/31/2010'
;
WITH
MyCTE /* http://technet.microsoft.com/en-us/library/ms175972.aspx */
(ShipName,ShipAddress,ShipCity,ShipRegion,ShipPostalCode,ShipCountry,CustomerID,CustomerName,[Address],
City,Region,PostalCode,Country,Salesperson,OrderID,OrderDate,RequiredDate,ShippedDate,ShipperName,
ProductID,ProductName,UnitPrice,Quantity,Discount,ExtendedPrice,Freight,ROWID) AS
(
SELECT
ShipName ,ShipAddress,ShipCity,ShipRegion,ShipPostalCode,ShipCountry,CustomerID,CustomerName,[Address]
,City ,Region,PostalCode,Country,Salesperson,OrderID,OrderDate,RequiredDate,ShippedDate,ShipperName
,ProductID ,ProductName,UnitPrice,Quantity,Discount,ExtendedPrice,Freight
, ROW_NUMBER() OVER (PARTITION BY CustomerID ORDER BY OrderDate , ProductName ASC) as ROWID /* Note that the ORDER BY (here) is directly related to the ORDER BY (near the very end of the query) */
FROM
dbo.Invoices inv /* 「Invoices」 is a VIEW, FYI */
where
(inv.OrderDate between @BeginDate and @EndDate)
)
SELECT
/*
ShipName,ShipAddress,ShipCity,ShipRegion,ShipPostalCode,ShipCountry,CustomerID,CustomerName,[Address],
City,Region,PostalCode,Country,Salesperson,OrderID,OrderDate,RequiredDate,ShippedDate,ShipperName,
ProductID,ProductName,UnitPrice,Quantity,Discount,ExtendedPrice,Freight,
*/
/*trim the list down a little for the final output */
CustomerID ,OrderID , OrderDate, (ExtendedPrice + Freight) as ComputedTotal
/*The below line is the 「trick」. I reference the above CTE, but only get data that is less than or equal to the row that I am on (outerAlias.ROWID)*/
, (Select SUM (ExtendedPrice + Freight) from MyCTE innerAlias where innerAlias.ROWID <= outerAlias.ROWID and innerAlias.CustomerID = outerAlias.CustomerID) as RunningTotal
, ROWID as ROWID_SHOWN_FOR_KICKS , OrderDate as OrderDate
FROM
MyCTE outerAlias
GROUP BY CustomerID ,OrderID, OrderDate, ProductName,(ExtendedPrice + Freight) ,ROWID,OrderDate
/*Two Order By Options*/
ORDER BY outerAlias.CustomerID , outerAlias.OrderDate , ProductName
/* << Whatever the ORDER BY is here, should match the 「ROW_NUMBER() OVER (ORDER BY ________ ASC)」 statement inside the CTE */
/*ORDER BY outerAlias.ROWID */ /* << Or, to keep is more 「trim」, ORDER BY the ROWID, which will of course be the same as the 「ROW_NUMBER() OVER (ORDER BY」 inside the CTE */
感謝您的回覆。 – sqlhurtsmyheadsometimes 2013-05-10 20:01:23
感謝您的回覆。 1)好點。我可以將插入的連接刪除到@tmpcustomers中。我正在尋找日期在日期範圍之前的訂單,因爲我基本上選擇了要在第二個查詢中排除的ID列表。我列出了在日期範圍開始之前已經有10個或更多訂單的人員的列表,然後將其從我的下一個查詢中排除。現在我實際上正在解釋發生了什麼,我認爲我認爲這部分太多了,可能沒有必要。 – sqlhurtsmyheadsometimes 2013-05-10 20:08:31
爲了闡明我的意圖 - 我想選擇在指定範圍內達到第n個總訂單的所有客戶。我還需要這些前n個總數的總和。 – sqlhurtsmyheadsometimes 2013-05-10 20:09:12