Question

I would like to ask about how can I take average of rows in a column that are between 5 minutes. In order to be more accurate I have a table like this

id-----link_id---------date---------------------speed 
0---------123------(24/4/2014 12:03:34)----------45 
1---------123------(24/4/2014 12:04:34)----------43 
2---------127------(24/4/2014 12:04:37)----------50 
3---------123------(28/4/2014 12:03:34)----------60 

i would like to create a new table that will have the average of speed for rows that have the same link_id and are between 5 minutes In the case that I mentioned above only the two first rows comply the requirements

and i want a new table like this

id-----link_id---------date---------------------speed 
0---------123------(24/4/2014 12:00:00)----------44 
2---------127------(24/4/2014 12:00:00)----------50 
3---------123------(28/4/2014 12:00:00)----------60 

which is the query that i have to use to create a new table with those requirments?

thank you in advance

Was it helpful?

Solution

It is not clear what you mean by 'average of speed for rows that ... are between five minutes.' So I will guess.

I guess you want to compute the averages for each distinct five minute interval. For example, you want averages of all items with timestamps from 2014-04-24 12:00:00 to 2014-04-24:12:04:59, then another average for items with timestamps from 2014-04-24 12:05:00 to 2014-04-24:12:09:59, and so forth.

To do this, you need to start with an expression that will take any DATETIME value and round it down to the beginning of its five-minute interval. How do you get that?

First, this expression will round down a timestamp to the beginning of the minute in which it occurs:

  DATE_FORMAT(`date`,'%Y-%m-%d %H:%i:00')

This expression gives the number of minutes past the hour, modulo 5.

  MINUTE(`date`)%5

So, this expression gives you the rounded-down DATETIME you need:

  DATE_FORMAT(`date`,'%Y-%m-%d %H:%i:00') - INTERVAL (MINUTE(`date`)%5) MINUTE

Great. Now we need to use that in an aggregate query to compute the average speeds.

SELECT link_id, 
       DATE_FORMAT(`date`,'%Y-%m-%d %H:%i:00') - INTERVAL (MINUTE(`date`)%5) MINUTE AS five_min
       AVG(speed) AS avg_speed
  FROM mytable
 GROUP BY link_id, 
          DATE_FORMAT(`date`,'%Y-%m-%d %H:%i:00') - INTERVAL (MINUTE(`date`)%5) MINUTE
 ORDER BY link_id, 
          DATE_FORMAT(`date`,'%Y-%m-%d %H:%i:00') - INTERVAL (MINUTE(`date`)%5) MINUTE

This will do the trick you need done. There will be one row for each distinct link_id and five-minute interval of time. The time interval will be named by giving the time at which it begins. Each row will contain the average speed for observations in that time interval.

It's helpful when creating your specification for this kind of query to think very carefully about what you want each row of your result set to contain. If you do that, you will probably find that your query flows naturally from your specification.

Here's a more extensive writeup on how to do this sort of thing.

http://www.plumislandmedia.net/mysql/sql-reporting-time-intervals/

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top