Reputation: 75
I'm trying to solve #13 on http://www.sqlzoo.net/wiki/The_JOIN_operation
"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."
Here is what I wrote:
select distinct mdate, team1,
case
when teamid = team1
count(teamid)
end as score1, team2,
case
when teamid = team2
count(teamid)
end as score2
from game join goal on id = matchid
group by matchid
order by mdate, matchid, team1, team2;
I am getting this error:
You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'count(teamid) end as score1, team2, case when teamid = tea' at line 4
what is wrong with my code?
Upvotes: 1
Views: 5222
Reputation: 7766
you r on wrong track with case condition and using functions..below query give you result..check and learn hope it will help
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
Upvotes: 1