473,608 Members | 2,425 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

SELECT multiple rows with average of a range of values

Hi All,

I've been racking my brain trying to figure out some sort of Sub-Select
mySQL statement that will create a result with multiple rows of
averaged values over a years time.

What I have is weather data. There is a new record every 5 minutes,
every day. So. What I want to do with one SQL statement is figure out
the Average of those 5 minute records over each day, for every day of
the year.

I've tried what seems now like a thousand different queries but i'm not
grasping something, I'm falling down when I try to figure out how to
continue selecting the range of records each day. I humbly ask if
someone here could make sense of it.

My dataset is very simple.

Table: archive
Fields: RecordTime (format is 20060101 120000), OutTemp

If I were to take another stab at it pseudo-code like it I'd do
something like

SELECT * FROM archive Where RecordTime is 2006 AND (SELECT AVG(OutTemp)
WHERE RecordTime is between 00:00 and 24:00)

It's figuring out how to replace the hours with something that COUNTS
(?) every instance of that range?

I'm confusing myself more just typing this message.

I should go lie down or something :)

Thank you any and all for your help.

Chris

Dec 18 '06 #1
2 13548

ch******@gmail. com wrote:
Hi All,

I've been racking my brain trying to figure out some sort of Sub-Select
mySQL statement that will create a result with multiple rows of
averaged values over a years time.

What I have is weather data. There is a new record every 5 minutes,
every day. So. What I want to do with one SQL statement is figure out
the Average of those 5 minute records over each day, for every day of
the year.

I've tried what seems now like a thousand different queries but i'm not
grasping something, I'm falling down when I try to figure out how to
continue selecting the range of records each day. I humbly ask if
someone here could make sense of it.

My dataset is very simple.

Table: archive
Fields: RecordTime (format is 20060101 120000), OutTemp

If I were to take another stab at it pseudo-code like it I'd do
something like

SELECT * FROM archive Where RecordTime is 2006 AND (SELECT AVG(OutTemp)
WHERE RecordTime is between 00:00 and 24:00)

It's figuring out how to replace the hours with something that COUNTS
(?) every instance of that range?

I'm confusing myself more just typing this message.

I should go lie down or something :)

Thank you any and all for your help.

Chris
Wouldn't you want something like:

SELECT DATE( RECORDTIME )
DAY , AVG( outtemp )
FROM archive
GROUP BY DATE( RECORDTIME )

?

Dec 19 '06 #2
Thank you very much strawberry!

I was looking at the DATE function after I sent my original message and
your suggestion has really simplified things.

For posterity... here's the final SQL statement that I came up with
last night to grab all of the weather data from the past Year, AVG it
by day, and spit it out with the date formated as "Jan 12" so that I
can use it for graphing.

SELECT ROUND((AVG(OutT emp)),2), DATE_FORMAT(DAT E(RecordTime),' %b
%d')DAY FROM archive WHERE RecordTime >= DATE_SUB(CURREN T_DATE,
INTERVAL 1 YEAR) GROUP BY DATE(RECORDTIME ) ;

http://www.alberniweather.ca/Yearly.php

If anyone has any suggestions on optimizations, it'd be greatly
appreciated, but my feeling is that this is about as good as it gets,
it is certainly querying very nice and fast.

Thanks

Chris
strawberry wrote:
ch******@gmail. com wrote:
Hi All,

I've been racking my brain trying to figure out some sort of Sub-Select
mySQL statement that will create a result with multiple rows of
averaged values over a years time.

What I have is weather data. There is a new record every 5 minutes,
every day. So. What I want to do with one SQL statement is figure out
the Average of those 5 minute records over each day, for every day of
the year.

I've tried what seems now like a thousand different queries but i'm not
grasping something, I'm falling down when I try to figure out how to
continue selecting the range of records each day. I humbly ask if
someone here could make sense of it.

My dataset is very simple.

Table: archive
Fields: RecordTime (format is 20060101 120000), OutTemp

If I were to take another stab at it pseudo-code like it I'd do
something like

SELECT * FROM archive Where RecordTime is 2006 AND (SELECT AVG(OutTemp)
WHERE RecordTime is between 00:00 and 24:00)

It's figuring out how to replace the hours with something that COUNTS
(?) every instance of that range?

I'm confusing myself more just typing this message.

I should go lie down or something :)

Thank you any and all for your help.

Chris

Wouldn't you want something like:

SELECT DATE( RECORDTIME )
DAY , AVG( outtemp )
FROM archive
GROUP BY DATE( RECORDTIME )

?
Dec 20 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1653
by: rg | last post by:
Hello, I use invisionboard on a bid web site (20 000 visitors each day) But my server has high load average. Could you help me to optimize these select command. 1) SELECT p.*, m.id,m.name,m.mgroup,m.email,m.joined,m.avatar,m.avatar_size,m.posts,m.aim_name,m.icq_number,
7
2820
by: Sunny K | last post by:
Hi guys, whilst working on a project which I thought was nearly complete I have come across a problem which was some how over seen, which I am hoping one of you guys know how to resovle. Firstly here is the script to create the table and insert some sample data:
13
4220
by: quickcur | last post by:
Suppose I have a function rand() that can generate one integer random number between 0 and 100. Suppose also rand() is very expensive. What is the fastest way to generate 10 different random number between 0 and 100? (call rand() only 10 times...) Thanks, qq
4
1308
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 number of dates and use FromOADate() to get back to a date, but it does not produce the desired results (as I expected, but still was hopefull it might).
4
4073
by: Ed L. | last post by:
I think I'm seeing table-level lock contention in the following function when I have many different concurrent callers, each with mutually distinct values for $1. Is there a way to reimplement this function using select-for-update (or equivalent) in order to get a row-level lock (and thus less contention) while maintaining the function interface? The docs seem to suggest so, but it's not clear how to return the SETOF queued_item and also...
33
6639
by: Peter | last post by:
People are telling me it is bad to put select * from <atable> in a view. I better should list all fields of the table inside the definition of the view. I dont know exactly why but some say: A select * from makes sql server does a table scan.
20
188289
by: talktozee | last post by:
Hey, everyone! Basically, I need to insert *multiple rows* into table A from table B based upon some criteria, and I need to insert some static values along with each row from table A. For example: insert into tableA (col1,col2,col3,col4,col5) values ('Cheese', 'Blue',
21
3237
by: Bill Cunningham | last post by:
I have create these 2 files. Called main.c and atr.c. They seem to work pretty well. I just wanted to submit them to see what if any errors others that know more might find. Thanks. atr.c #include <stdio.h> double atr (double th, double tl, double wc)
4
1842
by: scalda34 | last post by:
This in Access 2003. Pardon me if this is in the wrong spot, but I hoped someone could help. I have a table with multiple records that have a text represented date field. The date is in a format like: "06/01/2009" I need to select all the fields from the table but only the 6 oldest rows that fall in a range for each group of "DocType, PayTo, ContactName, ContactNumber, DocFooter, PQBName, LetterDate, RetireeFirstName, RetireeLastName,...
0
8501
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8157
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
6820
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6015
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5479
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3967
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4030
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2477
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1607
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.