我试图列出球员的职业生涯平均值,但仅包括在2018年至少参加过一场比赛的球员,但我的问题只是回归那些在2018年打过'仅'的球员,而不是职业平均数。所有在2018年至少参加过一场比赛的球员。
SELECT PlayerID
, Surname
, AKA
, COUNT(*) AS Played
, ROUND(AVG(Kicks),1) AS K
, ROUND(AVG(Handballs),1) AS HB
, ROUND(AVG(Marks),1) AS M
, Season
FROM `PlayerDetails`
WHERE Team = 'Team A'
GROUP
BY PlayerID
HAVING Season = 2018
ORDER
BY Surname ASC
答案 0 :(得分:2)
您需要删除
brew cleanup
子句并添加到HAVING
子句:
WHERE
您所拥有的SELECT PlayerID, Surname,AKA, COUNT(*) AS Played, ROUND(AVG(Kicks),1) AS K, ROUND(AVG(Handballs),1) AS HB, ROUND(AVG(Marks),1) AS M, Season
FROM `PlayerDetails` p1
WHERE Team = 'Team A'
AND EXISTS (SELECT * FROM `PlayerDetails` p2 WHERE p2.playerID = p1.playerID AND Season = 2018)
GROUP BY PlayerID
ORDER BY Surname ASC
子句 - 正如您所发现的那样 - 仅将查询限制为2018年的结果。我添加到HAVING
的{{1}}子句会找到任何有在2018年开始。