Let’s just say I have two columns, DATE and VALUE. The dates are not necessarily regularly spaced intervals. How can I create a moving average on VALUE over the past N days? I’m using postgres but mysql solutions would also be helpful.
Data:
DATE VALUE
2012-11-05 10
2012-10-29 31
2012-10-22 108
2012-10-17 3654
2012-10-16 1187
2012-10-15 12033
2012-10-09 41
2012-10-01 85
2012-09-25 20
2012-09-24 285
2012-09-17 20
2012-09-10 20
2012-09-04 41
2012-08-27 63
2012-08-20 52
2012-08-13 160
The MySQL example below covers a sliding 7-day window:
SQL Fiddle Example
Output: