Solved

SQL Query Average Over Time

Posted on 2014-04-23
5
540 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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Suggested Solutions

Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

861 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