Here's the basic query for grouping by an interval of 30 minutes.
SELECT FROM_UNIXTIME(ROUND(UNIX_TIMESTAMP(timestamp)/(30* 60)) * (30*60)) thirtyHourInterval FROM tablename GROUP BY ROUND(UNIX_TIMESTAMP(timestamp)/(30* 60));
Note. ROUND()
may cause an incorrect exit. Use the following query instead. Take a look at the following example:
SELECT ROUND(3.7), ROUND(4.2);
Result: 4 4
.
Both are in the same segment. The same applies to the query above, while rounding the timestamp
different segments may fall into the same segment, which leads to an incorrect exit
[Recommended next request]
SELECT FROM_UNIXTIME((UNIX_TIMESTAMP(`timestamp`) DIV (30* 60) ) * (30*60)) thirtyHourInterval FROM tablename GROUP BY UNIX_TIMESTAMP(`timestamp`) DIV (30* 60)
SQL FIDDLE DEMO
Alternatively, you can accept the following request.
SELECT FROM_UNIXTIME(ROUND(UNIX_TIMESTAMP(timestamp)/(30* 60)) * (30*60)) thirtyHourInterval FROM tablename GROUP BY ( 4 * HOUR( `timestamp` ) + FLOOR( MINUTE( `timestamp` ) / 30 ));
Related party
source share