Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL query to compare individual average with a group average

Tags:

sql

mysql

I have a simple table with names and a score. What I would like to do is produce a list of names with their average score, where their average is greater than the average of the group.

I have the following SQL statement that works in Access but not my server (running MySql):

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

the server is saying that the syntax is wrong but I can't see where. Any ideas? Thanks

like image 713
RGriffiths Avatar asked Jan 01 '26 17:01

RGriffiths


1 Answers

You don't need the first ; since it is a statement terminator.
Change this :

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

with this :

 SELECT Person, Avg(Score) AS PersonAverage FROM TblScores GROUP BY Person HAVING (((Avg(Score))>(SELECT Avg(Score) AS AverageOfAllScores FROM TblScores)));
like image 95
phadaphunk Avatar answered Jan 03 '26 09:01

phadaphunk



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!