php - SQL SUM values from column and dividide by number of rows
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
COUNTto get the totalNumber of rows.
* 1.0on the query will allow decimal places on the result.
or simply as
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).
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.