datetime - MySQL GROUP BY end of period -
i trying average 'value' column grouped 4 hour end periods (4am, 8am, 12pm). using mysql version 5.7.
an example table , data below:
create table testdata( timestamp datetime, value float ); insert testdata (timestamp, value) values ('2017-08-15 00:00:00', rand()), ('2017-08-15 00:15:00', rand()), ('2017-08-15 00:30:00', rand()), ('2017-08-15 00:45:00', rand()), ('2017-08-15 01:00:00', rand()), ('2017-08-15 01:15:00', rand()), ('2017-08-15 01:30:00', rand()), ('2017-08-15 01:45:00', rand()), ('2017-08-15 02:00:00', rand()), ('2017-08-15 02:15:00', rand()), ('2017-08-15 02:30:00', rand()), ('2017-08-15 02:45:00', rand()), ('2017-08-15 03:00:00', rand()), ('2017-08-15 03:15:00', rand()), ('2017-08-15 03:30:00', rand()), ('2017-08-15 03:45:00', rand()), ('2017-08-15 04:00:00', rand()), ('2017-08-15 04:15:00', rand()), ('2017-08-15 04:30:00', rand()), ('2017-08-15 04:45:00', rand()), ('2017-08-15 05:00:00', rand()), ('2017-08-15 05:15:00', rand()), ('2017-08-15 05:30:00', rand()), ('2017-08-15 05:45:00', rand()), ('2017-08-15 06:00:00', rand()), ('2017-08-15 06:15:00', rand()), ('2017-08-15 06:30:00', rand()), ('2017-08-15 06:45:00', rand()), ('2017-08-15 07:00:00', rand()), ('2017-08-15 07:15:00', rand()), ('2017-08-15 07:30:00', rand()), ('2017-08-15 07:45:00', rand()), ('2017-08-15 08:00:00', rand()), ('2017-08-15 08:15:00', rand()), ('2017-08-15 08:30:00', rand()), ('2017-08-15 08:45:00', rand()), ('2017-08-15 09:00:00', rand()), ('2017-08-15 09:15:00', rand()), ('2017-08-15 09:30:00', rand()), ('2017-08-15 09:45:00', rand()), ('2017-08-15 10:00:00', rand()), ('2017-08-15 10:15:00', rand()), ('2017-08-15 10:30:00', rand()), ('2017-08-15 10:45:00', rand()), ('2017-08-15 11:00:00', rand()), ('2017-08-15 11:15:00', rand()), ('2017-08-15 11:30:00', rand()), ('2017-08-15 11:45:00', rand()), ('2017-08-15 12:00:00', rand()), ('2017-08-15 12:15:00', rand()), ('2017-08-15 12:30:00', rand()), ('2017-08-15 12:45:00', rand()), ('2017-08-15 12:45:00', rand());
i want round next time period (therefore using greater than). when run following query:
select timestamp, round(avg(value), 3) value testdata timestamp > '2017-08-15 00:00:00' , timestamp <= '2017-08-15 12:00:00' group unix_timestamp( timestamp ) div 14400; -- 14400 seconds = 4 hours
this returns:
+---------------------+-------+ | timestamp | value | +---------------------+-------+ | 2017-08-15 00:15:00 | 0.244 | | 2017-08-15 01:00:00 | 0.478 | | 2017-08-15 05:00:00 | 0.439 | | 2017-08-15 09:00:00 | 0.499 | +---------------------+-------+ 4 rows in set (0.00 sec)
what like:
+---------------------+-------+ | timestamp | value | +---------------------+-------+ | 2017-08-15 04:00:00 | 0.244 | | 2017-08-15 08:00:00 | 0.478 | | 2017-08-15 12:00:00 | 0.439 | +---------------------+-------+ 3 rows in set (0.00 sec)
could please let me know how possible?
thanks.
query
select timestamp , round(avg(value), 3) value testdata hour(timestamp) >= 4 , hour(timestamp) <= 12 group floor(hour(timestamp) / 4)
result
timestamp value ------------------- -------- 2017-08-15 04:00:00 0.500 2017-08-15 08:00:00 0.441 2017-08-15 12:00:00 0.527
Comments
Post a Comment