473,394 Members | 1,722 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,394 software developers and data experts.

Averaging Data by Date

bergy
89
Background:

We are collecting data and pushing it to MySQL at various intervals. Sometimes it may be every minute, other times every 15 minutes, and all ranges in between. This data is timestamped using the UNIX epoch timestamp (if needed I could convert this to a MySQL timestamp).

When viewing the data in just one day, it is okay to show all rows, but we run into a problem with load times when we go past say a week... (at 1440 rows per day a week contains 10,080 rows - on top of that if we're looking at multiple columns say 3, we're now at 30,000+ pieces of data). This is really bogging down our graph displays.

Question:

I'm wondering if there is way to say find the average of rows between X period, so if we're looking at an entire year, I can average and sum the minutely data to hourly or daily data. I know there is an AVG function, but the way I understand it is that that would average all given rows, I want to be able to average all rows between X and Y have that as a returned row from the query and then continue to average between the next X and Y. I hope that makes sense.

I guess one way to attack it is multiple queries, but I'd like to be able to do this in a single query if it's possible. If we're looking at daily averages over a year it would be nice to not have to submit 365 separate queries to the database - not to mention the math/logistics in figuring out X date starts at Y timestamp and ends at Z timestamp.

The idea is to make this query code/platform independent so whether we're accessing data via PHP or Java we don't have to write separate logic on each platform to decide how the queries are going to look.

Any ideas on attacking this problem would be welcomed - Thanks a bunch!
Jan 23 '09 #1
2 9191
Atli
5,058 Expert 4TB
Hi.

You can use the date functions and the GROUP BY clause to group the rows into days or weeks or whatever you need, and return an AVG just for that period.

For example:
Expand|Select|Wrap|Line Numbers
  1. SELECT 
  2.     AVG(`Value`) AS `Average`,
  3.     YEAR(`Date`) AS `Year`,
  4.     MONTHNAME(`Date`) AS `Month`
  5. FROM `random`
  6. GROUP BY `Year`, `Month`;
Would give you and average for each month in that table.
Like:
Expand|Select|Wrap|Line Numbers
  1. +---------+------+-----------+
  2. | Average | Year | Month     |
  3. +---------+------+-----------+
  4. | 50.5726 | 2007 | April     | 
  5. | 50.4852 | 2007 | August    | 
  6. | 49.9294 | 2007 | December  | 
  7. | 50.0309 | 2007 | February  | 
  8. | 50.0602 | 2007 | January   | 
  9. | 50.4556 | 2007 | July      | 
  10. | 49.4132 | 2007 | June      | 
  11. | 50.4012 | 2007 | March     | 
  12. | 49.9563 | 2007 | May       |
  13. etc...
I tested this using a integer value field that has a random value for every 15 minutes over the span of 4 years. The query executed in ~0.25s on my test server. (Which is a 5 year old gamer PC, by the way)
Jan 23 '09 #2
bergy
89
Atli,

This is perfect thanks - I'm assuming I'll have to convert dates into how MySQL likes them, but that shouldn't be a big deal. We're holding data on an embedded industrial PC so it's good to know your query times on your 5 year old machine. Thanks again!
Jan 23 '09 #3

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

Similar topics

3
by: captain | last post by:
Below is the sql for data with same date need to extract + or - 5 days data of same date also. How to also get data of + and - days related to same date. SELECT IM.Area, IM.Location,...
1
by: Rotten Spice | last post by:
Hello all, I am still pretty new to Access but I do have a grasp on some basic functions. I am having a problem with averaging and I think I'm trying to do something a little too complex and...
0
by: NicK chlam via DotNetMonster.com | last post by:
this is the error i get System.Data.OleDb.OleDbException: Syntax error in INSERT INTO statement. at System.Data.Common.DbDataAdapter.Update(DataRow dataRows, DataTableMapping tableMapping) at...
4
by: Matt Hamilton | last post by:
I have a query that returns multiple dates and I want to find the average date... How can I do this? I tried to use the ToOADate() to get the total of the dates as a double, then divide by the...
17
by: Justin Emlay | last post by:
I'm hopping someone can help me out on a payroll project I need to implement. To start we are dealing with payroll periods. So we are dealing with an exact 10 days (Monday - Friday, 2 weeks). ...
2
by: Michael Hamm | last post by:
I have the following XML file (simplified from the actual): <r> <o><n>1</n><si>s</si><v1>1</v1><v2>2</v2><v3>3</v3></o> <o><n>2</n><si>i</si><v1>4</v1><v2>5</v2><v3>6</v3></o>...
1
by: JJ R | last post by:
I am trying to develop a simple and efficient to calculate averages. For example I want to calculate the averages of the Values for rows 1 and 2 and use ID 1 as the row with the average result. ...
0
by: troy_lee | last post by:
I have a report based on a query. It is a simple listing of returned merchandise that has been repaired and shipped. I have a text box in the detail section that calculates the total days the...
1
by: Kid Programmer | last post by:
Hello guys. Whenever I am learning a new programming language I like to learn the basics (variables, loops, conditionals etc) by writing a program that will calculate averages. I wrote a simple...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
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...

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.