TABLE 1
ID | DATE |
a | 10-07-2012 |
b | 10-07-2012 |
c | 10-07-2012 |
TABLE2
ID | OrdersID |
a | 001 |
b | 002 |
c | 003 |
TABLE3
ItemID | OrdersID | Items |
1 | 001 | 5 |
2 | 002 | 3 |
7 | 003 | 2 |
3 | 003 | 7 |
4 | 001 | 8 |
2 | 003 | 7 |
5 | 003 | 6 |
6 | 003 | 1 |
TABLE4
ItemID | ItemName |
1 | ABC |
2 | EFG |
3 | HIJ |
4 | KLM |
5 | NOP |
6 | QRS |
7 | TUV |
I would like to select the ID’s from TABLE1 t where Date = 10-07-2012
In Table2 with the ID’s I would like to select the OrdersID‘s
With the OrdersID‘s I would like to add together how many Items of a ItemID exist e.g. IteamID 2 has a total of 10 Items
From TABLE3 I would like to know the top 5 Items and the quantity of Items in Order and get the names of the the ItemID from TABLLE4 ItemName.
Expected results 5 results
ItemID | ItemName | Quantity |
2 | EFG | 10 |
3 | HIJ | 7 |
.....
.....
.....
.....
SQL used at the start …
SELECT SUM (t3.Items) , t4.ItemName
FROM Table3 t3
JOIN Table2 t2 ON t3.OrdersID = t2.OrdersID
JOIN Table1 t1 ON t2.ID = t1.ID
WHERE t1.[Date] = '10072012 00:00:00'
Depending on how you want to break ties in your top five, this is a working answer.
Here’s the output I get. Notice that Item 4 ranks higher than Item 3 which does not match with the sample output specified.