Skip to content
Advertisement

SQL JOIN with 2 aggregates returning incorrect results

I am trying to join 3 different tables to get how many Home Runs a player has in his career along with how many Awards they have recieved. However, I’m getting incorrect results:

Peoples PlayerId

Battings PlayerId, HomeRuns

AwardsPlayers PlayerId, AwardName

Current Attempt

Result

I am pretty confident it’s my second join Do I need to do some sort of subquery or should this work? Barry Bonds definitely does not have 35,814 Home Runs nor does he have 1,034 Awards

If I just do a single join, I get the correct output:

What am I doing wrong? I’m sure it’s how I’m joining my second table (AwardsPlayers)

Advertisement

Answer

I think you have two independent dimensions. The best approach is to aggregate before joining:

Result

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement