I have a table test containing data with 1 minute step, here is an extract of it:
| DATE_TIME | VALUE_G |
|---|---|
| 2016-01-01 00:30:00 | 0.0 |
| 2016-01-01 00:31:00 | 0.0 |
| 2016-01-01 00:32:00 | 0.0 |
| 2016-01-01 00:33:00 | 0.0 |
| 2016-01-01 00:34:00 | 0.0 |
| 2016-01-01 00:35:00 | 0.0 |
| 2016-01-01 00:36:00 | 0.0 |
| 2016-01-01 00:37:00 | 0.0 |
| 2016-01-01 00:38:00 | 0.09 |
| 2016-01-01 00:39:00 | 0.8 |
| 2016-01-01 00:40:00 | 1.1 |
| 2016-01-01 00:41:00 | 1.1 |
| 2016-01-01 00:42:00 | 1.1 |
| 2016-01-01 00:43:00 | 0.77 |
| 2016-01-01 00:44:00 | 0.37 |
| 2016-01-01 00:45:00 | 0.37 |
| 2016-01-01 00:46:00 | 0.37 |
| 2016-01-01 00:47:00 | 0.52 |
| 2016-01-01 00:48:00 | 0.65 |
| 2016-01-01 00:49:00 | 0.4 |
| 2016-01-01 00:50:00 | 0.27 |
I want to get the average of VALUE_G every 10 minutes, but I want the average to be calculated like this:
| DATE_TIME_AGG | AVG(VALUE_G) |
|---|---|
| 2016-01-01 00:30:00 | 0.0 |
| 2016-01-01 00:40:00 | 0.199 |
| 2016-01-01 00:50:00 | 0.592 |
In the above example, for the first row, the average is calculated for DATE_TIME between "2016-01-01 00:21:00" and "2016-01-01 00:30:00", in the second row : between "2016-01-01 00:31:00" and "2016-01-01 00:40:00" and in the third row between "2016-01-01 00:41:00" and "2016-01-01 00:50:00". How can I achieve this knowing that the table test contains a lot of data.
Following this answer https://stackoverflow.com/a/4073342/15648345 I can get part of the work done but, the average is not calculated as I want. Here is the code :
select from_unixtime(ROUND(unix_timestamp(DATE_TIME) / (60*10)) * 60 * 10) as DATE_TIME_AGG ,AVG(VALUE_G)
from test
group by DATE_TIME_AGG;