全联盟 [英] Union all
问题描述
有人可以帮我解决这个问题吗?
我有两张桌子。我想在两列中添加值并获得总计。
例如$
表1
PID     数量     salqty
211    200      50
表2
PID    数量  是¥b $ b 210   10   2016年
211    5    2016年
212    2    2016年
211    3    2015年b
我想从这两张桌子中获得2016年Pid 211的总计(总计155)zh
我的编码如下:
选择Pid,Sum(Table1.qty-Salqty)
来自表1
其中Table1.pid = table2.pid
和Pid分组
UNION ALL
选择Table2.pid,sum(Table2.qty)
来自表2
按Table2.pid分组
感谢帮助
选择A.pid,A.qty + B表格1中的.qty-A.salQty
A.pid上的内部联接表B = B.Pid
其中b.yr = 2016
Can anybody help me in solving this problem
I have two tables. I wanted to add the values in two columns and to get a grand total.
e.g
Table 1
pid qty salqty
211 200 50
Table 2
pid qty yr
210 10 2016
211 5 2016
212 2 2016
211 3 2015
I wanted to get the grand total of Pid 211 for the year 2016 from these two tables (Grand Total 155)
I am coding the same as follows:
Select Pid, Sum(Table1.qty-Salqty)
from Table1
where Table1.pid=table2.pid
and Group by Pid
UNION ALL
Select Table2.pid, sum(Table2.qty)
from Table2
Group by Table2.pid
Appreciate help
select A.pid,A.qty+B.qty-A.salQty from Table1 A Inner Join Table B on A.pid=B.Pid where b.yr=2016
这篇关于全联盟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!