Solved

SQL Query Average Over Time

Posted on 2014-04-23
5
534 Views
Last Modified: 2014-05-15
Hello,

I have a basic table in MySQL

Timestamp - datetime
Value - double

I have data for every minute stored in this table, for example:

1/1/2014 12:00:00
1/1/2014 12:01:00
1/1/2014 12:02:00 ....

I'd like to write a query that will return the average 'Value' of each hour, for the past 60 hours.  

Any suggestions on the best approach?
0
Comment
Question by:compsol1993
  • 2
  • 2
5 Comments
 
LVL 109

Assisted Solution

by:Ray Paseur
Ray Paseur earned 250 total points
ID: 40018766
To get the value of 60 hours ago, you might use something like this:

$ago = date('c', strtotime('NOW - 60 HOURS'));

Your query would have a WHERE clause saying something like:

WHERE myTimestamp > '$ago'

You can get the hour value from the myTimestamp column with the HOUR() function.

You can get the average value from a column with the AVG() function.

I don't have a test data set to give you an example, but it seems like you can write a GROUP clause to group the average values by the hours.  If you want to post a CREATE TABLE statement and a test data set that provides easy verification, I'll be glad to write a small script that loads the table and runs the query.
0
 
LVL 48

Accepted Solution

by:
PortletPaul earned 250 total points
ID: 40019405
over a 60 hour period you would need to cater for both DATE() and HOUR()

e.g.

select
  date(`myTimestamp`) as day_of
, hour(`myTimestamp`) as hour_of
, avg(`some_value`) as avg_value
from tab1
where myTimestamp > '$ago' --  borrowing from above
group by
  date(`myTimestamp`)
, hour(`myTimestamp`)
0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 40020078
@PortletPaul: Good point!  I might have come to that realization when tried to test and discovered that days have fewer than 60 hours!

:-)

Ray
0
 
LVL 48

Expert Comment

by:PortletPaul
ID: 40020247
:) happy to chip in Ray. Cheers.
0
 

Author Closing Comment

by:compsol1993
ID: 40068030
Thanks!
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses how to create an extensible mechanism for linked drop downs.
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to count occurrences of each item in an array.

808 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question