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
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
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.