sql 在案例中使用聚合函数进行查询
sql query using aggregate function inside case
我正在尝试解决 http://www.sqlzoo.net/wiki/The_JOIN_operation
上的 #13
"List every match with the goals scored by each team as shown. This will use "CASE WHEN”,这在之前的任何练习中都没有解释过。”
这是我写的:
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;
我收到这个错误:
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
我的代码有什么问题?
你在案例条件和使用函数方面走错了路..下面的查询给你结果..检查并学习希望它会有所帮助
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
我正在尝试解决 http://www.sqlzoo.net/wiki/The_JOIN_operation
上的 #13"List every match with the goals scored by each team as shown. This will use "CASE WHEN”,这在之前的任何练习中都没有解释过。”
这是我写的:
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;
我收到这个错误:
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
我的代码有什么问题?
你在案例条件和使用函数方面走错了路..下面的查询给你结果..检查并学习希望它会有所帮助
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