php - SQL SUM values from column and dividide by number of rows
492
I'm trying to calculate a percentual...and I need to sum all values from a column (Its already ok) but I need divide the result by number of rows...
select sum(voto) from tablename where id = numberHere
Answer
Solution:
use
COUNT
to get the totalNumber of rows.by adding
* 1.0
on the query will allow decimal places on the result.or simply as
Answer
Solution:
JW's answer is correct if you're looking specifically to do it by Summing/Dividing, but SQL has a function for that.
AFAIK, it automatically returns the same type as you input (except date columns, which should be parsed to seconds then re-encoded to date).
Answer
Solution:
AVG should work, count(*) should work, you can also use @@rownum to get the number of rows returned by the statement if you need to do more with that number.