Group mysql query by 15 min intervals

前端 未结 10 827
醉梦人生
醉梦人生 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:51

    Adaptation of approach 1) below:

    select Round(date_format(date, "%i") / (15*60)) AS interval  
    from table
    group by interval 
    

    Adaptation of approach 3) below:

    SELECT Round(Convert(substring(date_column, 14, 2), UNSIGNED) / (15*60)) AS interval /* e.g. 2009-01-04 12:20:00 */
    FROM table 
    GROUP BY interval;
    

    A few approaches I've found here:

    1)

    select date_format(date, "%W") AS `Day of the week`, sum(cost)
    from daily_cost
    group by `Day of the week` 
    order by date_format(date, "%w")
    

    2)

    select count(*) as 'count', 
      date_format(min(added_on), '%Y-%M-%d') as 'week commencing',
      date_format(added_on, '%Y%u') as 'week' 
    from system 
    where added_on >= '2007-05-16' 
    group by week 
    order by 3 desc;
    

    3)

    SELECT substring(postdate, 1,10) AS dd, COUNT(id) FROM MyTable GROUP BY dd;
    

    (Also here: http://www.bradino.com/mysql/dayparting-on-datetime-field-using-substring/)

    EDIT: All the solutions will perform badly on a table with a large number of records.

提交回复
热议问题