SQLzoo JOIN tutorial #13

五迷三道 提交于 2021-02-18 16:53:17

问题


I have been working on the SQLzoo problems but having trouble with the last one in the JOIN tutorial question #13 - List every match with the goals scored by each team as shown.

Link: http://sqlzoo.net/wiki/The_JOIN_operation

In the sample code they gave, it uses a case. I modified it like this:

SELECT game.mdate, game.team1,
  CASE WHEN goal.teamid=game.team1 THEN 1 ELSE 0 END score1, game.team2,
  CASE WHEN goal.teamid=game.team2 THEN 1 ELSE 0 END score2
  FROM game, goal WHERE game.id=goal.matchid
GROUP BY game.mdate, goal.matchid, game.team1, game.team2

They suggest that using a SUM function on the score1/score2 will provide the answer. I am confused as to how to use the SUM function on these 2 columns that are created within the SQL.

Could anyone provide a hint as to how to do or mention in broad terms how to write this SQL query in a better fashion?


回答1:


Well, you do need to SUM those columns (SUM is an aggregation function, that's why you have a GROUP BY there). And as to writing your query in a better fashion, you need to lose that old implicit JOIN style and use the ANSI explicit one:

SELECT  game.mdate, 
        game.team1,
        SUM(CASE WHEN goal.teamid=game.team1 THEN 1 ELSE 0 END) score1, 
        game.team2,
        SUM(CASE WHEN goal.teamid=game.team2 THEN 1 ELSE 0 END) score2
FROM game
INNER JOIN goal 
    ON game.id=goal.matchid
GROUP BY game.mdate, goal.matchid, game.team1, game.team2



回答2:


This worked for me with correction of left join.

SELECT mdate, team1,
SUM(CASE WHEN teamid = team1 THEN 1 ELSE 0 END) score1,
team2,
SUM(CASE WHEN teamid = team2 THEN 1 ELSE 0 END) score2
FROM game LEFT JOIN goal ON matchid = id GROUP BY mdate, matchid, team1, team2



回答3:


Your SQL is pretty good, you just need a good example of the SUM aggregate function.
Here is my solution:

 SELECT mdate,
 team1, SUM(CASE game.team1 WHEN goal.teamid THEN 1 ELSE 0 END) 'score1',
 team2, SUM(CASE game.team2 WHEN goal.teamid THEN 1 ELSE 0 END) 'score2'
 FROM game LEFT JOIN goal ON game.id=goal.matchid
 GROUP BY game.id
 ORDER BY mdate, matchid, team1, team2

Note: My original post gave an incorrect value, need to use LEFT JOIN instead of INNER JOIN. The left join takes care of the case when both teams have 0 scores, the inner join does not pick up those cases.




回答4:


I think the SQLzoo problem was updated and the above answers do not appear to fully solve question #13. Note the sorting ORDER BY clause is now needed.

SELECT
        mdate,
        team1,
        SUM(CASE                  
            WHEN teamid=team1 THEN 1                              
            ELSE 0              
        END) as 'score1',
        team2,
        SUM(CASE                  
            WHEN teamid=team2 THEN 1                  
            ELSE 0              
        END) as 'score2'             
    FROM
        game          
    LEFT JOIN
        goal                  
            ON (
                goal.matchid = game.id                 
            )          
    GROUP BY
        mdate,
        team1,
        team2          
    ORDER BY
        mdate,
        matchid,
        team1,
        team2     ;


来源:https://stackoverflow.com/questions/15439874/sqlzoo-join-tutorial-13

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!