1internet Posted March 7, 2013 Share Posted March 7, 2013 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? Quote Link to comment https://forums.phpfreaks.com/topic/275343-averaging-sql-row-results/ Share on other sites More sharing options...
requinix Posted March 7, 2013 Share Posted March 7, 2013 (edited) Use AVG() but make sure to GROUP BY the company ID - otherwise MySQL will average out all the values. Edited March 7, 2013 by requinix Quote Link to comment https://forums.phpfreaks.com/topic/275343-averaging-sql-row-results/#findComment-1417078 Share on other sites More sharing options...
Psycho Posted March 7, 2013 Share Posted March 7, 2013 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 Quote Link to comment https://forums.phpfreaks.com/topic/275343-averaging-sql-row-results/#findComment-1417086 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.