桌面游戏
Home | Visitor | Spectators
-----------------------------------
a | b | 12
b | c | 15
a | c | 32
b | a | 3
c | a | 8
表格小组
id | name
--------------
a | TeamA
b | TeamB
c | TeamC
现在我需要计算每支球队的总观众数(主场+参赛者)
答案 0 :(得分:0)
是否需要按照表格团队显示的方式进行格式化?如果没有,这应该适合你:
y_true
这将输出为:
select top 1
(select sum(spectators) from sheet1 where home='a' or visitor='a') as teama,
(select sum(spectators) from sheet1 where home='b' or visitor='b') as teamb,
(select sum(spectators) from sheet1 where home='c' or visitor='c') as teamc
from sheet1;
答案 1 :(得分:0)
有很多方法可以做到,这是另一种方法:
SELECT name,sum(sum)
FROM
(SELECT t.name,SUM(g.spectators)
FROM teams t, games g WHERE t.id=home
GROUP BY t.name
UNION
SELECT t.name,SUM(g.spectators)
FROM teams t, games g WHERE t.id=visitor
GROUP BY t.name)visits
GROUP BY name