
[英]Mysql - calculate difference between two columns in 2 different tables and insert
[英]Need the difference between two columns ' different in number of items ' in two different tables in MS Access
我需要我购买的物品和我出售的物品,我在此表中购买的物品之间的区别
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
您没有提供属性名称或表名称,而是指定了两种RDBMS产品,因此这是您的解决方案的p代码:
q1:SELECT ItemName,SUM(购买)为从tBought GROUP BY ItemName中购买的总和
q2:SELECT ItemName,从tSold GROUP BY ItemName作为SumSold的SUM(sold)
q3:选择q1.ItemName,(SumBought-SumSold)作为与q1.ItemName = q2.ItemName上q1内部联接q2的差异
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.