I need the difference between items i bought and items i sold, items i bought in this table
ID |ItemName| PriceOfUnit | NumberOfItems I bought |DateIBought|
1 | tea | 3 | 6 |15/11/2015 |
2 | coffee | 5 | 4 |16/11/2015 |
3 | tea | 4 | 10 |20/12/2015 |
4 | juice | 5 | 15 | 1/1/2016 |
5 | coffee | 3 | 5 | 15/3/2016 |
6 | water | 5 | 2 | 16/4/2016 |
and items I sold is in this table
ID |ItemName| PriceOfUnit | NumberOfItems I sold |DateIBought|
1 | coffee | 5 | 6 | 1/1/2016 |
2 | tea | 5 | 9 | 15/3/2016 |
3 | coffee | 4 | 2 | 20/4/2016 |
4 | juice | 5 | 11 | 1/1/2016 |
I need a query , SQL query OR union Query in MS Access to get this result:
ID |ItemName| NumberOfItems I have |
1 | coffee | 1 |
2 | tea | 7 |
3 | juice | 4 |
4 | water | 2 |
Where NumberOfItems I have = NumberOfItems I bought - NumberOfItems I sold
i tried this
q1:
SELECT ItemName, SUM(bought) as SumBought
FROM tBought GROUP BY ItemName
q2:
SELECT ItemName, SUM(sold) as SumSold
FROM tSold GROUP BY ItemName
q3:
SELECT q1.ItemName, (SumBought - SumSold) as difference
FROM q1 inner join q2 on q1.ItemName = q2.ItemName
and i get this result
ID |ItemName| NumberOfItems I have |
1 | coffee | 1 |
2 | tea | 7 |
3 | juice | 4 |
all i need now .. is to display NumberOfItems I bought
if i didn't sold any thing of this item
as this example was " water "