我有3个表: 1.Parent,2.Child1Table和3.Child2Table
家长
ParentItem soldQty
----------------------
111 5
222 10
333 4
Child1Table
ParentItem ChildItemID1 soldQty
-------------------------------------
555 551 5
222 221 10
333 331 14
Child2Table
ParentItem ChildItemID2 soldQty
-------------------------------------
555 552 5
666 662 10
333 332 20
期待输出
ParentItem QtySold
---------------------
111 5 //5
222 20 //10 + 10
333 38 //4 + 14 + 20
555 10 //5 + 5
666 10 //10
是否可以使用FULL OUTER JOIN
实现此目的?
答案 0 :(得分:1)
UNION ALL
您的表。 GROUP BY
结果:
select ParentItem, sum(soldQty) as QtySold
from
(
select ParentItem, soldQty from Parent
union all
select ParentItem, soldQty from Child1Table
union all
select ParentItem, soldQty from Child2Table
) dt
group by ParentItem
答案 1 :(得分:1)
答案 2 :(得分:0)
由于表中只有ParentItem
,只需将数据集合并,然后聚合。
select parentitem, sum(soldqty) as qtysold
from
(
select parentitem, soldqty from parent
union all
select parentitem, soldqty from child1table
union all
select parentitem, soldqty from child2table
) alldata
group by parentitem;