Was the OrderItem column a typo, then?Can we do this in Northwind, changing OrderItems to "Order Details", keeping OrderID, and changing ProductCode to ProductID?That would give you (using your revised code):SELECT [T_ProductID1] = O1.ProductID, [T_ProductID2] = O3.ProductID, [T_Count] = COUNT(*)FROM dbo."Order Details" O1 -- Find another order that has this product JOIN dbo."Order Details" O2 ON O2.ProductID = O1.ProductID AND O2.OrderID <> O1.OrderID -- Find other products on the second order JOIN dbo."Order Details" O3 ON O3.OrderID = O2.OrderID AND O3.ProductID <> O1.ProductIDGROUP BY O1.ProductID, O3.ProductIDORDER BY T_Count DESC
Which gives a result starting:31 16 35021 61 304
There are 51 orders containing ProductID 31, and 7 orders with both 31 and 16, so (51-1)*7 = 350,There are 39 orders containing ProductID 21, and 8 orders containing both 21 and 61, so (39-1)*8 = 304.Etc.I kind of assumed you'd want something more likeSELECT [T_ProductID1] = O1.ProductID, [T_ProductID2] = O3.ProductID, [T_Count] = COUNT(*)FROM dbo."Order Details" O1 -- Find other products on the same order JOIN dbo."Order Details" O3 ON O3.OrderID = O1.OrderID AND O3.ProductID > O1.ProductIDGROUP BY O1.ProductID, O3.ProductIDORDER BY T_Count DESC
With either > or <> depending on whether you wanted each pairs to appear in both ways round.