473,396 Members | 1,683 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

How to pull hourly averages for a week's worth of data (5 minute intervals) in MySQL?

OS: Ubuntu Linux
PHP: v5.3.2
MySQL: v5.1.41

Hello all,

I'm new to Bytes, but I'm hoping that someone here will be able to help me as I learn PHP and MySQL. With any luck, I'll understand it enough that I may be able to help others in the future.

Hopefully I posted this to the right section (it's for a PHP file that makes a call to a MySQL database) - my apologies if I should have went to the MySQL area. As the title states, I am trying to pull the hourly averages for a week's worth of data that was taken at 5 minute intervals from a MySQL database. So that the situation clear, I'll explain what I am attempting to do. I have a database that stores temperature data to a table with five columns (dtkey, time, SerialNumber, and Fahrenheit). Data is collected every five minutes with the result below:

Expand|Select|Wrap|Line Numbers
  1. dtKey     time             SerialNumber         Fahrenheit
  2. -------------------------------------------------------------------
  3. 1409     2010-06-23 00:00:02     10C4A9F501080055     73.51
  4. 1410     2010-06-23 00:05:02     10C4A9F501080055     72.85
  5. 1411     2010-06-23 00:10:02     10C4A9F501080055     72.51
  6. 1412     2010-06-23 00:15:02     10C4A9F501080055     73.51
  7. 1413     2010-06-23 00:20:02     10C4A9F501080055     73.85
  8. 1414     2010-06-23 00:25:02     10C4A9F501080055     73.51
  9. 1415     2010-06-23 00:30:02     10C4A9F501080055     73.85
  10. 1416     2010-06-23 00:35:02     10C4A9F501080055     73.85
  11. 1417     2010-06-23 00:40:03     10C4A9F501080055     73.96
  12. 1418     2010-06-23 00:45:02     10C4A9F501080055     73.74
  13. 1419     2010-06-23 00:50:02     10C4A9F501080055     73.85
  14. 1420     2010-06-23 00:55:03     10C4A9F501080055     72.72
  15. 1421     2010-06-23 01:00:02     10C4A9F501080055     73.74
  16. 1422     2010-06-23 01:05:02     10C4A9F501080055     72.39
  17. 1423     2010-06-23 01:10:03     10C4A9F501080055     73.96
  18. 1424     2010-06-23 01:15:02     10C4A9F501080055     73.51
  19. 1425     2010-06-23 01:20:02     10C4A9F501080055     73.74
  20. 1426     2010-06-23 01:25:03     10C4A9F501080055     74.08
  21. 1427     2010-06-23 01:30:02     10C4A9F501080055     73.96
  22. 1428     2010-06-23 01:35:02     10C4A9F501080055     73.96
  23. 1429     2010-06-23 01:40:02     10C4A9F501080055     73.85
  24. 1430     2010-06-23 01:45:03     10C4A9F501080055     69.91
  25. 1431     2010-06-23 01:50:02     10C4A9F501080055     67.78
  26. 1432     2010-06-23 01:55:02     10C4A9F501080055     66.65
  27. 1433     2010-06-23 02:00:03     10C4A9F501080055     65.86
  28. 1434     2010-06-23 02:05:02     10C4A9F501080055     65.75
  29. ...and so on.
  30.  
I am trying to chart this data using PHP and Flash on a webpage. The webpage will allow you to choose which of the eight charts you would like to load (Yesterday, Today, Last Week, This Week, Last Month, This Month, Last Year, or This Year).

I've already gotten the Yesterday and Today charts working with these codes:

Yesterday
$result = mysql_query("SELECT * FROM digitemp WHERE DATE_SUB(CURRENT_DATE, INTERVAL 1 DAY) < time ORDER BY time ASC LIMIT 288");
Today
$result = mysql_query("SELECT * FROM digitemp WHERE time > CURRENT_DATE ORDER BY time ASC");
So for the "This Week" chart, I'm trying to come up with a line code that will take at all of the data from the current week (Sunday through Saturday not the past 7 days) and calculate the average temperature for each hour. The result would have 24 temperature values for each day instead of 288 values, arranged in time order.

Based on the example data above, it would take the temperatures from midnight to 1 (aka 00:00:02 to 00:55:03), average it, and return a value of 73.48. The same would be done for 01:00:02 to 01:55:02 and so on. The values would then be arrange in time and day order (Sunday through Saturday). For the "Last Week" chart, it would be similar, but somehow subtract the current week's days.

I've tried a bunch of different lines, but the most recent attempt was:
SELECT AVG( Fahrenheit ), time FROM temparchive WHERE YEARWEEK(time) = YEARWEEK(CURRENT_DATE) GROUP BY HOUR( time ) ORDER BY time ASC

However, this does not work.

Does anyone have thoughts on how to do this? Would it be a simple matter to adapt the code for the 'This Week' and 'Last Week' charts to the 'This Month' and 'Last Month' charts?

Thank you for any help you can offer!

Regards,
Nick
Jun 25 '10 #1

✓ answered by improvcornartist

Maybe group by date, then hour:
SELECT AVG( Fahrenheit ), time FROM temparchive WHERE YEARWEEK(time) = YEARWEEK(CURRENT_DATE) GROUP BY DATE( time ), HOUR( time ) ORDER BY time ASC

4 11630
code green
1,726 Expert 1GB
However, this does not work
It is not clear whether the problem is extracting data for the relevant period or getting the averages
I think you need to seperate out the specific problem, maybe break it down into two.
See if can be done via MySQL.
If not just get all data into PHP arrays which can easily be manipulated
Jun 25 '10 #2
The problem seems to be getting the proper time period.

Running:
SELECT AVG( Fahrenheit ), time FROM temparchive WHERE YEARWEEK(time) = YEARWEEK(CURRENT_DATE) GROUP BY HOUR( time ) ORDER BY time ASC

...does give me the a set of hourly averages, but it combines all the days into one. So while three days worth of data should give me 72 averages, it instead gives me only 24 averages where all of the 1AM's (or in this case 01:xx:xx) from the three days are merged into one and averaged.

If I go the route of:
SELECT AVG( Fahrenheit ), time FROM temparchive WHERE YEARWEEK(time) = YEARWEEK(CURRENT_DATE) GROUP BY DATE( time ) ORDER BY time ASC

...I get average of each day. So three days give me 3 averages. I simply can't figure out how to first divide the data into separate days, and then divide the resulting data by hours.
Jun 25 '10 #3
improvcornartist
303 Expert 100+
Maybe group by date, then hour:
SELECT AVG( Fahrenheit ), time FROM temparchive WHERE YEARWEEK(time) = YEARWEEK(CURRENT_DATE) GROUP BY DATE( time ), HOUR( time ) ORDER BY time ASC
Jun 25 '10 #4
@improvcornartist
Thank you very much!

That did the trick perfectly. I had tried "DATE(HOUR(time))" and "HOUR(DATE(time))" but never tried "DATE(time), HOUR(time)".

This has been one of those very frustrating problems with a simple solution that was just escaping me. I can now move forward with setting up the other graphs and adding new sensors.

Once again, thank you.
Jun 25 '10 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: David List | last post by:
I am wondering what I miss to be able to handle binary data from the mysql client. I have ensured that the user has file_priv set to 'Y' and that max_allowed_packet is larger that the binary lumps...
4
by: Rick Barr | last post by:
I'm trying to build a tee-time reservation page, and want to display all the available tee times on one page for a particular date. I simply want to list the times out in 10-minute intervals,...
4
by: Bryan Powell | last post by:
Is it possible to grab data from a MySQL database and print it on the screen with VC++? The database is on a linux server if that would make any difference.
2
by: jenese | last post by:
Hi! I've searched the forum and the net for the answer of, probably an easy question for the experienced.... I need to limit the amount of text to 50 first characters when looping out the data...
1
by: ariel gons | last post by:
This is one of my project and also my thesis in school. How can I get data on database MYSQL using javascript function? Is there anybody here can help me with this problem.. Can anyone give...
1
by: gubbachchi | last post by:
Hi, For my project I have a "add" button, upon clicking it, it will take the user to next page where there will be a text box and list of data displayed below it which is fetched from mysql...
7
by: sathyashrayan | last post by:
Dear group, A mysql wrapper class, which just uses the CRUD operations for large scale php development. Is the same advisable for the mysql stored procedure? Following is a link I got from...
2
by: ipab | last post by:
I'm a complete Python/MySQL newbie - trying to retrieve and manipulate data from a MySQL DB over a VPN/LAN from an Ubuntu/Python shell. I've imported all the modules I think I need - MySQLdb,...
1
by: raziya | last post by:
give me the details about data types in mysql..
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.