我有两个表readysale
和ordersale
。我想得到两个表的总和。表列如 t1=pid,branch,quantity
和 t2=pid,branch,quantity
。我需要分支列中所有分支的列表,并显示总金额为readysale,ordersale
。 某些分公司他们没有做好销售准备或订购,但它应该显示在列表中0。总结两列
Q
总结两列
-1
A
回答
1
这将让来自两个表的总量联合在一起的分支分组:
select sales.branch, sum(sales.quantity) as quantity
from (
select branch, quantity
from readysale
union
select branch, quantity
from ordersale
) as sales
group by sales.branch
1
select sum(u.quantity) as total, u.branch
from (
select quantity, branch
from readysale
union
select quantity, branch
from ordersale
) as u
group by u.branch
编辑:
然后
select u.itemcode, u.branch, sum(u.ordersaleqty), sum(u.readysaleqty)
from (
select itemcode, branch, 0 as ordersalqty, quantity as readysaleqty
from readysale
union
select itemcode, branch, quantity as ordersalqty, 0 as readysaleqty
from ordersale
) as u
group by u.itemcode, u.branch
,或者使用完全外部联接
select
coalesce(r.itemcode, o.itemcode),
coalesce(r.branch, o.branch),
sum (r.quantity) as readysaleqty,
sum (o.quantity) as ordersaleqty
from readysale r
full outer join ordersale o on o.branche = r.branch
group by coalesce(r.itemcode, o.itemcode), coalesce(r.branch, o.branch);
+0
谢谢你的回复,但我正在寻找像itemcode,brach,readysaleqty,ordersalequantity – user1453189
+0
@ user1453189好的,编辑好的列表。 –
相关问题
- 1. mysql总结两列
- 2. 总结两列了Unix的
- 3. 总结两个总结语句
- 4. LINQ到基于组总结两列
- 5. 总结两个非数字列
- 6. 问题查询总结两列
- 7. 总结两列之间的行
- 8. 平均两列的总结在Excel
- 9. 总结列
- 10. Dplyr总结列
- 11. SQL总结列
- 12. 总结多列
- 13. 总结列
- 14. 总结来自两个不同表格的两列
- 15. MySQL:两列总和
- 16. 试图总结错误消息在SQL Server结果两列
- 17. XSLT总结两个属性
- 18. 总结两个DataFrames由Index
- 19. 总结两个条件
- 20. 两列两列的总和mysql mysql
- 21. dplyr总结多列
- 22. 条件总结列
- 23. 总结GridView列值
- 24. 没有总结列
- 25. ActiveRecord的总结列
- 26. 总结2D阵列
- 27. 在特定的列上总结两个具有0值的列
- 28. 如何通过两列中的外键来总结列?
- 29. 如何两列的每个字段总结成一列
- 30. 如何总结每列两列的乘积
请尽量说明一点。 – fancyPants
请花一些时间来显示样本表和数据,如果你想让人们花时间在你的问题 – ClearLogic