I want to calculate a 12 month moving average from a MySQL column. The data represents time-series power measurements, it is a largish dataset (every 10 minutes for several years). A high performance query would be nice but speed is something I can work out later.
DTE Active
2012-1-3 00:10 500
2012-1-3 00:20 520
... etc
The following query gives me the total Active for each month :
SELECT YEAR(DTE) AS year, MONTH(DTE) AS month, SUM(Active)/6 as total FROM saturne s GROUP BY YEAR(DTE), MONTH(DTE)
The following query gives me the moving average Active for a given month and year - say october 2011
SELECT SUM(Active)/6 AS average FROM saturne
WHERE (YEAR(DTE) = 2011 AND MONTH(DTE) <= 10) OR (YEAR(DTE) = 2010 AND MONTH(DTE) > 10)
I would however like to generate a query which returns the monthly total and the 12 month moving average in the next column.
year month total average
2012 2 701474 9258089
2012 1 877535 9386664
... etc
(The factor of 6 is because the data represents instantaneous power recorded every 10 minutes, dividing the total by 6 gives the total energy)