My table consists of all order history like this:
table: orderhistory
OrderID ProductID CustomerID Quantity
1 4 1 1
1 5 1 2
1 6 1 1
2 4 2 1
2 3 2 1
2 5 2 1
OrderID and ProductID are primary keys.
So purchase history related to product 4 consists of product 3, 5 and 6. And product 5 should be prior to other records as it appears twice together with 4 in a single order. How can I use a single query to grab that information?
Desired result: (query for related history of product 4)
ProductID Freq
5 2
6 1
3 1