如何从三个表中获得总和? [英] How to get the Sum from three tables?
问题描述
可能的重复:
如何从两个表中获取总和?
我有三个表,第一个products
"第二个items
"第三个sales
"
第一个和第二个表具有相同的列 ('code
','quantity
') 但第三个表具有 ('code
','name
') 现在我想要第一个和第二个的总和数量,但还想从第三个表中获取代码相同的名称.检查我的代码
I have three table first "products
" second "items
" third "sales
"
The first and second tables have the same columns('code
','quantity
') but the third table has ('code
','name
') now I want sum quantity from first and second but want also want get name from third table which code is equal.
check my code
Select code, sum(qtd),name
from ( select a.code, a.qtd from product a
union all select b.code, b.qtd from items b
union all select c.name from sales c where b.code=c.code
)
group by code
前两个给了我完美的值,但第三个小说给出了错误,没有显示名字.
first two giving me perfect values but third fiction giving error not showing also names.
推荐答案
我觉得你想做这样的事情,虽然不清楚实际需求是什么:
I think you want to do something like this, though it's not clear what the actual requirements are:
select code = t1.code ,
name = t2.name ,
qtd = t1.qtd
from ( select code = t.code ,
qtd = sum( t.qtd )
from ( select a.code, a.qtd from product a
union all select b.code, b.qtd from items b
) t
group by t.code
) t1
join sales t2 where t2.code = t1.code
干杯!
这篇关于如何从三个表中获得总和?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!