连接表并返回正确的聚合

时间:2018-07-01 22:46:33

标签: sql sql-server join

我正在使用Sean Lahman的棒球数据库来汇总2010年至2015年之间各队的奔跑,命中和“击球”,胜利与失败。我想加入Teams和Batting表格,并在teamID上使用按功能分组从“团队”表中返回团队的总奔跑次数,击中次数,击球次数以及团队的得失。

例如,我要从“团队”表中逐年返回赢利和亏损

team ID Name             Wins  Losses Year
ARI Arizona Diamondbacks    65  97  2010
ARI Arizona Diamondbacks    94  68  2011

在击球表中,这是我想要的输出

year   teamID Runs Hits  At Bats
2012    ARI 734   1416  5462
2015    ARI 720  1494   5649

我尝试了以下查询,但是它返回了获胜和失败列的膨胀值:

select b.yearID, b.teamID, SUM(b.R) as Runs, SUM(b.H) as Hits, SUM(b.AB) as At_Bats, 
t.name as Team_Name, SUM(t.W) as Wins, SUM(t.L) as Losses
from Batting b, Teams t
where b.teamID = t.teamID and b.yearID=t.yearID and b.yearID between '2010' and '2015'
group by b.teamID, b.yearID, t.name, t.W, t.L
order by b.teamID 

可以在此处找到数据库http://www.seanlahman.com/files/database/readme2017.txt的文档

1 个答案:

答案 0 :(得分:1)

尽管我在文档中找不到它,但我猜想teamID和yearID的每种组合都唯一标识Teams表中的记录。在总结赢与输时,您可以将它们乘以相关玩家的数量。所以不要在t.W和t.L上求和:

select b.yearID, b.teamID, SUM(b.R) as Runs, SUM(b.H) as Hits, SUM(b.AB) as At_Bats, 
t.name as Team_Name, t.W as Wins, t.L as Losses
from Batting b, Teams t
where b.teamID = t.teamID and b.yearID=t.yearID and b.yearID between '2010' and '2015'
group by b.teamID, b.yearID, t.name, t.W, t.L
order by b.teamID