Group mysql query by 15 min intervals

前端 未结 10 875
醉梦人生
醉梦人生 2020-11-27 03:09

I\'ve got a monitoring system that is collecting data every n seconds (n is approximately 10 but varies). I\'d like to aggregate the collected data by 15 minute intervals.

10条回答
  •  清歌不尽
    2020-11-27 03:46

    Unix timestamps: floor them to nearest 15 minute using one of the following:

    timestamp div (15 * 60) * (15 * 60) -- div is integer division operator
    timestamp - timestamp % (15 * 60)
    

    Date time: assuming the datatype does not have fractional seconds, floor them to nearest 15 minute using:

    date - INTERVAL EXTRACT(SECOND FROM date) SECOND - INTERVAL EXTRACT(MINUTE FROM date) % 15 MINUTE
    

    DBFiddle

提交回复
热议问题