I have a mysql database with which contains data in 5 minute bins. I’d like to create hourly average of the data starting on the half hour.
By using mysql built-in group by:
select date,AVG(AE) from mytable group by date(date),HOUR(date);
would compute average value from say, 01:00 to 02:00. Instead I would like hourly averages to be computed from 00:30 to 01:30, were the value would then be the hourly average at 01:00.
This query fail when a new day starts:
select date, AVG(AE) from mytable group by date(date), HOUR( date ) + FLOOR( MINUTE( date ) / 30 );
+---------------------+------------------+
| date | AVG(AE) |
+---------------------+------------------+
| 1997-01-01 22:30:00 | 23 |
| 1997-01-01 23:30:00 | 28.3 |
| 1997-01-02 00:00:00 | 20.1333333333333 |
| 1997-01-02 00:30:00 | 29.3 |
| 1997-01-02 01:30:00 | 27.5666666666667 |
| 1997-01-02 02:30:00 | 43.4166666666667 |
which is the closest I’ve gotten 🙂
In another post ( https://stackoverflow.com/a/6560742/1142735 ) it was suggested that GROUP BY FLOOR(MOD((mytimestamp-1800)/3600)) would create intervals starting on the half hour if timestamp was used. I am using datetime.
Thanks
Paul
Anything that uses the
DATE()function will fail to correctly group the interval23:30 - 00:30.Use: