Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL - select interval of every 2 hours from timestamp column

Tags:

sql

mysql

How do you select every n-hours in a timestamp column? I am able to do so with 1 hour by grouping:

SELECT log_timestamp
FROM log
GROUP BY 
    DATE( log_timestamp ), 
    HOUR( log_timestamp )

Returns:

log_timestamp
2015-11-08 00:00:01
2015-11-08 01:00:01
2015-11-08 02:00:01
2015-11-08 03:00:01
...

Any ideas for how to do this by 2 or more hours?

log_timestamp
2015-11-08 00:00:01
2015-11-08 02:00:01
2015-11-08 04:00:01
2015-11-08 06:00:01
...
like image 302
brietsparks Avatar asked Dec 14 '15 15:12

brietsparks


1 Answers

You can use INTEGER DIVISION:

SELECT L.log_timestamp 
FROM log L 
GROUP BY DATE( log_timestamp ), hour( log_timestamp ) DIV 2

SqlFiddleDemo

like image 137
Lukasz Szozda Avatar answered Sep 25 '22 17:09

Lukasz Szozda