one mysql query getting, per each row, the average of the previous three rows -
one mysql query getting, per each row, the average of the previous three rows -
i have this:
id | value --------------- 201311 | 10 201312 | 15 201401 | 20 201402 | 5 201403 | 17
and need result this:
201311 | null or 0 201312 | 3.3 // 10/3 201401 | 8.3 // (15+10)/3 201402 | 15 // (20+15+10)/3 201403 | 13.3 // (5+20+15)/3
so far, got point can avg of lastly 3 previous rows this:
select avg(c.value) (select b.value table b b.id < 201401 order b.id desc limit 3) c
passing id manually. i'm not able each id.
any ideas much appreciated!
thanks lot.
regards
i think you'll have write stored procedure, utilize cursor, iterate through table , populate new table using values calculated in cursor loop. if need help writing out cursor loop, drop comment , can example.
mysql
Comments
Post a Comment