Best way to Calculate Averages?

I could use some advice on whether there is a more streamlined way to get averages directly from MySQL.

I have this sample table…


rater	comment		commenter	score	date
------	-------		----------	-----	-----
user1	1001		doubledee	2	2013-09-16
user1	1002		doubledee	2	2013-09-17
user2	1002		doubledee	1	2013-09-17

I need to calculate two averages…

First, the Average Score of each Comment in the table, for example…


comment		avg score
--------	----------
1001		2
1002		1.5

Secondly, the Average Score for each Commenter in the table, for example…


commenter	avg score
----------	----------
doubledee	1.666

I was planning on using aggregate functions like SUM and COUNT and then calculate the AVG using PHP, but maybe this can all be done in SQL?

Also, it just occurred to me that maybe I need to use a GROUP BY function??

Getting confused on what initially looked like easy calculations… :blush:

Sincerely,

Debbie

There’s a built-in AVG function.

http://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html#function_avg

yes, you had better test that :wink: