我有像
这样的SQL连接select B.Team,Sum(A.Quota) as Quota from TableA A
inner join TableB B on B.code = A.TblCode
group by B.Team
给我结果
备用5000
Grace 2000
我有另一个像
这样的SQL连接select B.Team, Sum(C.Revenue) As Revenue from TableC C
inner join TableB B on B.code = c.StaCode
group by B.Team
以上sql的结果是
备用10000
Grace 12000
除了加入三个表之外,是否可以得到如下所示的结果?
备用5000 10000
Grace 2000 12000
我尝试连接三个表,这给了我不同的结果,因为其中一个表中有重复项。
答案 0 :(得分:1)
你可以JOIN
这两个结果
SELECT x.Team, X.Quota, Y.Revenue
FROM (select B.Team, Sum(A.Quota) as Quota
from TableA A
inner join TableB B on B.code = A.TblCode
group by B.Team) as x
JOIN (select B.Team, Sum(C.Revenue) As Revenue
from TableC C
inner join TableB B on B.code = c.StaCode
group by B.Team) as Y ON X.Team = Y.Team
答案 1 :(得分:0)
您可以使用相关子查询分别计算值:
SELECT Team,
(SELECT SUM(Quota)
FROM TableA
WHERE TblCode = TableB.code) AS Quota,
(SELECT SUM(Revenue)
FROM TableC
WHERE StaCode = TableB.code) AS Revenue
FROM TableB
(这相当于LEFT JOIN,但我怀疑如果另一个没有匹配,你想要删除一个配额/收入值。)