Group by 2 mysql pieces

Mysql table:

enter image description here

I want to take the amount of acceleCount at intervals of 2 minutes.

Query:

select  time div 120000 as TwoMinutes,                 
sum(acceleCount) as Sum
from acceleTable
group by time div 120000

      

Result:

enter image description here

Here, the timestamp of column twoMinutes is less. I want it to have a time stamp that is within two minutes.

Any thoughts on how to modify the SQL query?

+3


source to share


2 answers


Bring the timestamps to a common denominator by division, rounding and multiplication:



SELECT
(ROUND(time / 120) * 120),
sum(acceleCount)
FROM acceleTable
GROUP BY (ROUND(time / 120) * 120)

      

+1


source


A small optimized way to do it



SELECT    (ROUND(time / 120000) * 120000)  AS timekey, sum(acceleCount)
FROM       acceleTable
GROUP BY   timekey

      

+1


source







All Articles