diff --git a/SQL Zoo The_JOIN_operation 13.txt b/SQL Zoo The_JOIN_operation 13.txt new file mode 100644 index 0000000..cce0bb4 --- /dev/null +++ b/SQL Zoo The_JOIN_operation 13.txt @@ -0,0 +1,18 @@ +List every match with the goals scored by each team as shown. This will use "CASE WHEN" which has not been explained in any previous exercises. +mdate team1 score1 team2 score2 +1 July 2012 ESP 4 ITA 0 +10 June 2012 ESP 1 ITA 1 +10 June 2012 IRL 1 CRO 3 +... +Notice in the query given every goal is listed. If it was a team1 goal then a 1 appears in score1, otherwise there is a 0. You could SUM this column to get a count of the goals scored by team1. Sort your result by mdate, matchid, team1 and team2. + +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 id = matchid +GROUP BY mdate, team1, team2 +ORDER BY mdate, team1, team2 \ No newline at end of file