1SELECT
2 TH.TransactionID,
3 TH.ProductID,
4 TH.ReferenceOrderID,
5 TH.ReferenceOrderLineID,
6 TH.TransactionDate,
7 TH.TransactionType,
8 TH.Quantity,
9 TH.ActualCost
10FROM Production.TransactionHistory AS TH
11WHERE TH.ReferenceOrderID =
12(
13 SELECT TOP (1)
14 TH2.ReferenceOrderID
15 FROM Production.TransactionHistory AS TH2
16 WHERE TH2.TransactionType = N'P'
17 ORDER BY
18 TH2.Quantity DESC,
19 TH2.ReferenceOrderID ASC
20);