Vladimir Gordienko
Vladimir Gordienko

Reputation: 3470

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?

Upvotes: 0

Views: 392

Answers (1)

Vamsi Prabhala
Vamsi Prabhala

Reputation: 49260

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

Upvotes: 1

Related Questions