Vladimir Gordienko Vladimir Gordienko - 1 year ago 114
MySQL Question

MYSQL get AVG for each row

I need get AVG for each row of table. Lets say we have

id val
1 5
2 6
3 7

I need to get

id val
1 0.277 (5/18)
2 0.333 (6/18)
3 0.388 (7/18)

Can I easy get this in MYSQL without joining with the same table?

vkp vkp
Answer Source

You can sum up the val column and divide the val column by that sum.

select id, 1.0*val/(select sum(val) from tablename) val
from tablename
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download