I have a problem to calculate easily some simple average. My table :
id / user / action / data
1 / a / unit_price / 40
2 / a / quantity / 1
3 / b / unit_price / 70
4 / b / quantity / 2
Unit_price is a price for a user and quantity is quantity. So there i should get :
(40 + 70 + 70) / 3 = 60
If i do an
(AVG(action) WHERE action = unit_price)
I get :
(70+40)/2 = 55
If I do an
(SUM(action) WHERE action = unit_price) / (SUM(action) WHERE action = quantity)
I get :
110 / 3 = 36.6
The easiest way I found is to don’t put the unit_price but the global price then make a division in the PHP code to get the unit_price, but I was hoping SQL could do something for me.
SqlFiddle