我需要购买的商品和我销售的商品,我在此表中购买的商品之间的区别
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 |
我销售的商品在此表中
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 |
我需要在MS Access中使用查询,SQL查询或联合查询来获得此结果吗?
ID |ItemName| NumberOfItems I have |
1 | coffee | 1 |
2 | tea | 7 |
3 | juice | 4 |
4 | water | 2 |
我怎样才能得到这个结果?
NumberOfItems I have = NumberOfItems I bought - NumberOfItems I sold
答案 0 :(得分:0)
您没有提供属性名称或表名,并且您指定了两个RDBMS产品,因此这里是您的解决方案的p代码:
Q 1: SELECT ItemName,SUM(已购买)为来自tBought GROUP BY ItemName的SumBought
Q2: 从TSold GROUP BY ItemName中选择ItemName,SUM(已售出)为SumSold
Q3: SELECT q1.ItemName,(SumBought - SumSold)与q1.ItemName = q2.ItemName
上q1内连接q2的区别