# Thread: SUM and AVG for certain date ranges in a stored procedure?

1. Registered User
Join Date
Dec 2002
Posts
123

## Unanswered: SUM and AVG for certain date ranges in a stored procedure?

Hi,

I was just wondering if you have an optimal solution for this --

I have a table with meter readings for 5 days ( for only one meter). The table looks like something like this --

Meterid Date Val

m1 2008-05-14-00.00.00.000000 100
m1 2008-05-14-01.00.00.000000 150
m1 2008-05-14-02.00.00.000000 500
.
.
.
.
m1 2008-05-15-00.00.00.000000 200
m1 2008-05-15-01.00.00.000000 230
m1 2008-05-15-02.00.00.000000 120
.
.
.
.

and so on...

So i have data for 5 days in this table... I want to find the sum of Val for each day and find the three totals that are the highest.

For example, sum of 1 day = 1000, 2 day = 1500, 3 day = 2200, 4 day = 4000, 5 day = 2000

In this case, three highest totals correspond to day 3, 4 ,5

After finding the three days, I want to find the hourly average for this meter ( using only data from the 3 days)

I want the find the avg of day 3, 4 and 5's 00:00 value, 01:00 value, 02:00 value, etc

Is there a simple way to do this? Can the AVG function be used to calculate the averages of the same hour of different dates? Thanks!
Last edited by db2user; 05-14-08 at 15:50.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•