SQL查询将个人平均值与组平均值进行比较

时间:2013-06-12 19:43:31

标签: mysql sql

我有一个简单的表格,上面有名字和分数。我想做的是制作一份名单及其平均分数,其平均值大于该组的平均值。

我有以下SQL语句适用于Access但不适用于我的服务器(运行MySql):

SELECT Person, Avg(Score) AS PersonAverage FROM TblScores GROUP BY Person HAVING (((Avg(Score))>(SELECT Avg(Score) AS AverageOfAllScores FROM TblScores;))); 

服务器说语法错误但我看不到哪里。有任何想法吗?感谢

1 个答案:

答案 0 :(得分:1)

您不需要第一个;,因为它是一个语句终止符。
改变这个:

SELECT Person, Avg(Score) AS PersonAverage FROM TblScores GROUP BY Person HAVING (((Avg(Score))>(SELECT Avg(Score) AS AverageOfAllScores FROM TblScores;)));

用这个:

 SELECT Person, Avg(Score) AS PersonAverage FROM TblScores GROUP BY Person HAVING (((Avg(Score))>(SELECT Avg(Score) AS AverageOfAllScores FROM TblScores)));