Jump to content

averaging sql row results


1internet

Recommended Posts

Ok I have a table, called reviews.
I want to work out the average for where a company=84 for example
 
So we have all these numbers for reviews
 
company   review
84                  1
84                  2
84                  3
84                  4
84                  3
89                  4
89                  1
89                  4
 
I want to be able to add them all up, and divide them by the number of rows, to create the average for the reviews. But only for a particular company.
So in this example I would want to extract 1+2+3+4+3 = 13/5 = 2.6
 
I have been trying to use msyql AVG(), but not much luck.
How do I go about doing this?

 

Link to comment
https://forums.phpfreaks.com/topic/275343-averaging-sql-row-results/
Share on other sites

Use AVG() but make sure to GROUP BY the company ID - otherwise MySQL will average out all the values.

 

Right, but since he only wants the average of the rows where company = 84 he wouldn't need the GROUP BY, just a where clause

 

@1internet, this will give you the average for a particular office

 

SELECT AVG(review) WHERE company = 84

 

OR you could run a query to get the average of every office (using GROUP BY as Requinex suggested)

 

SELECT company, AVG(review) as average
GROUP BY company

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.