views:

400

answers:

2

I've got a monitoring system that is collecting data every n seconds (n ~=10 but varies). I'd like to aggregate the collected data by 15 minute intervals. Is there a way to corral the timestamp column into 15 minute chunks to allow for grouping to work?

+2  A: 

.

SELECT   ROUND(UNIX_TIMESTAMP(timestamp)/(15 * 60)) AS timekey
FROM     table
GROUP BY timekey;
unutbu
Shouldn't it be `UNIX_TIMESTAMP(timestamp)/(15 * 60)` for minutes?
Alexander Gladysh
Yes, thanks for the correction @Alexander and @nickf.
unutbu
That seems to do it, thanks for the quick response
gsiener
A: 

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.

MartyIX
well - how would you adapt these to group by 15 minute ranges?
nickf
The same way as ~unutbu did. But he was faster :-)
MartyIX