Solved

SQL Query Average Over Time

Posted on 2014-04-23
5
541 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 110

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 110

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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Suggested Solutions

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
When table data gets too large to manage or queries take too long to execute the solution is often to buy bigger hardware or assign more CPUs and memory resources to the machine to solve the problem. However, the best, cheapest and most effective so…
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

756 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