473,480 Members | 2,094 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Trying to calculate a rolling 12 month average ( was new to access)

22 New Member
Hello,

I'm new to Access and trying to calculate a rolling 12 month average from some time data. Each data point is a date and a measurement taken at that date. As far as I can tell, the only way to take the rolling average is to create a make-table of all the data points within the last year. Then create a query to pull out the minimum date, create a second query to pull out the maximum date, create a thrid query to pull out the measurement from the minimum date, and create a forth query to pull out the measurement for the maximum date. Finally, create a fith query to average the min/max measurements.

Seems like there should be a more efficient way to do this. Any suggestions?
Thanks!
Aug 26 '08 #1
12 9117
FishVal
2,653 Recognized Expert Specialist
Hello,

I'm new to Access and trying to calculate a rolling 12 month average from some time data. Each data point is a date and a measurement taken at that date. As far as I can tell, the only way to take the rolling average is to create a make-table of all the data points within the last year. Then create a query to pull out the minimum date, create a second query to pull out the maximum date, create a thrid query to pull out the measurement from the minimum date, and create a forth query to pull out the measurement for the maximum date. Finally, create a fith query to average the min/max measurements.

Seems like there should be a more efficient way to do this. Any suggestions?
Thanks!
Hello.

The same may be done by a single query with two subqueries.

Expand|Select|Wrap|Line Numbers
  1. SELECT 
  2. ((SELECT TOP 1 t.dblMeas FROM t
  3. WHERE Year(t.dteDate)=Year(Date()) ORDER BY t.dteDate)+
  4. (SELECT TOP 1 t.dblMeas FROM t
  5. WHERE Year(t.dteDate)=Year(Date()) ORDER BY t.dteDate DESC))/2 AS Expr1
  6. FROM t;
  7.  
Regards,
Fish
Aug 26 '08 #2
denveromlp
22 New Member
Hello.

The same may be done by a single query with two subqueries.

Expand|Select|Wrap|Line Numbers
  1. SELECT 
  2. ((SELECT TOP 1 t.dblMeas FROM t
  3. WHERE Year(t.dteDate)=Year(Date()) ORDER BY t.dteDate)+
  4. (SELECT TOP 1 t.dblMeas FROM t
  5. WHERE Year(t.dteDate)=Year(Date()) ORDER BY t.dteDate DESC))/2 AS Expr1
  6. FROM t;
  7.  
Regards,
Fish
Thanks Fish, very helpful.

Although I didn't quite describe the averaging clearly. I don't want to take the average of the two measurements, I actually want to take the time derivative. Instead of (meas1+meas2)/2, I want (meas2-meas1)/(date2-date1). To find out at what rate the measurement is increasing.
Aug 26 '08 #3
FishVal
2,653 Recognized Expert Specialist
Thanks Fish, very helpful.

Although I didn't quite describe the averaging clearly. I don't want to take the average of the two measurements, I actually want to take the time derivative. Instead of (meas1+meas2)/2, I want (meas2-meas1)/(date2-date1). To find out at what rate the measurement is increasing.
Ok.

Next time please don't hesitate to call a thing by its name :D
In what units you need to calculate time differential - hours, days, months?
Aug 26 '08 #4
denveromlp
22 New Member
Ok.

Next time please don't hesitate to call a thing by its name :D
In what units you need to calculate time differential - hours, days, months?
Looking to use days.
Aug 26 '08 #5
missinglinq
3,532 Recognized Expert Specialist
"New to Access" tells our members nothing of your problem, and so I have changed the title. Please, in the future, remember to provide a meaningful Title for any threads you start! This helps to ensure that other members, and also the general public, will have a better chance of finding answers to similar questions. It is difficult for the experts to answer questions when there is not enough details provided to understand the problem.

Please take the time to read the Posting Guidelines, paying particular attention to Give a clear title to your question.


Thank you and Welcome to Bytes!!

Linq ;0)>

Moderator
Aug 26 '08 #6
FishVal
2,653 Recognized Expert Specialist
Looking to use days.
Expand|Select|Wrap|Line Numbers
  1. SELECT TOP 1 
  2. ((SELECT TOP 1 t.dblMeas FROM t WHERE Year(t.dteDate)=Year(Date())
  3. ORDER BY t.dteDate)+
  4. (SELECT TOP 1 t.dblMeas FROM t WHERE Year(t.dteDate)=Year(Date())
  5. ORDER BY t.dteDate DESC))/
  6. (SELECT DateDiff("d",Min(t.dteDate),Max(t.dteDate)) FROM t
  7. WHERE Year(t.dteDate)=Year(Date())) AS Expr1
  8. FROM t;
  9.  
More elegant it could be implemented using VBA.

Regards,
Fish
Aug 26 '08 #7
denveromlp
22 New Member
ok, we're getting closer...thanks for your help with this

I pulled out all of the "WHERE Year(t.dteDate)=Year(Date())" clauses because the data in the table is already only data for the previous 12 months. Also, it is a rolling 12 months, not a calendar year, so some of the data is from the last part of 2007 and the rest from 2008. After removing where statements, seems to be running correctly.

The only problem is that the data should be grouped by another field, call it fieldX. I need to look at the measurement from the earliest date and latest date within the different entries of fieldx, not on the whole table.

make sense?
Aug 26 '08 #8
FishVal
2,653 Recognized Expert Specialist
Please include the MetaData for all relevant datasets with clear explanation of important fields purpose. Here is an example of how to post table MetaData :
Table Name=tblStudent
Expand|Select|Wrap|Line Numbers
  1. Field; Type; IndexInfo
  2. StudentID; AutoNumber; PK
  3. Family; String; FK
  4. Name; String
  5. University; String; FK
  6. Mark; Numeric
  7. LastAttendance; Date/Time
Aug 27 '08 #9
denveromlp
22 New Member
Please include the MetaData for all relevant datasets with clear explanation of important fields purpose. Here is an example of how to post table MetaData :
Table Name=tblStudent
Expand|Select|Wrap|Line Numbers
  1. Field; Type; IndexInfo
  2. StudentID; AutoNumber; PK
  3. Family; String; FK
  4. Name; String
  5. University; String; FK
  6. Mark; Numeric
  7. LastAttendance; Date/Time
1. Unit Number, Numeric
2. Download Date, Date/Time
3. MWHRS, Numeric

For each unit number, there are many download date's each with one measurement of MWHRS. For each unit, I need to find the minimum download date and associated MWHRS, and the maximum download date and associated MWHRS. Then calculate (MWHRS_2-MWHRS_1)/(DownloadDate_2-DownloadDate_1).

The previous code took the maximum download date out of all of the units, the minimum download date out of all of the units and the corresponding MWHRS. So I ended up getting the same output for each unit number.
Aug 27 '08 #10
FishVal
2,653 Recognized Expert Specialist
Ok.

As I've already posted - SQL/VBA hybrid solution will be preferrable.

In code module:
Expand|Select|Wrap|Line Numbers
  1. Public Function Derivative(lngUnitNumber As Long) As Double
  2.  
  3.     Dim rs As New ADODB.Recordset
  4.     Dim dteTemp As Date, dblTemp As Double
  5.  
  6.     With rs
  7.  
  8.         'opens recordset filtered by particular unit number ordered by date
  9.         .ActiveConnection = CurrentProject.Connection
  10.         .CursorType = adOpenDynamic
  11.         .LockType = adLockOptimistic
  12.         .Open "SELECT * FROM t WHERE lngUnitNumber=" & _
  13.             lngUnitNumber & " ORDER BY dteDate;"
  14.  
  15.         'get values of first record (earliest date)
  16.         dteTemp = !dteDate
  17.         dblTemp = !dblMWHRS
  18.  
  19.         'move to last record (latest date) and calculate derivative
  20.         .MoveLast
  21.         Derivative = (!dblMWHRS - dblTemp) / DateDiff("d", dteTemp, !dteDate)
  22.         .Close
  23.  
  24.     End With
  25.  
  26.     Set rs = Nothing
  27.  
  28. End Function
  29.  
Query:
Expand|Select|Wrap|Line Numbers
  1. SELECT DISTINCT t.lngUnitNumber, Derivative(t.lngUnitNumber) AS Derivative
  2. FROM t;
  3.  
Regards,
Fish
Aug 28 '08 #11
denveromlp
22 New Member
Ok.

As I've already posted - SQL/VBA hybrid solution will be preferrable.

In code module:
Expand|Select|Wrap|Line Numbers
  1. Public Function Derivative(lngUnitNumber As Long) As Double
  2.  
  3.     Dim rs As New ADODB.Recordset
  4.     Dim dteTemp As Date, dblTemp As Double
  5.  
  6.     With rs
  7.  
  8.         'opens recordset filtered by particular unit number ordered by date
  9.         .ActiveConnection = CurrentProject.Connection
  10.         .CursorType = adOpenDynamic
  11.         .LockType = adLockOptimistic
  12.         .Open "SELECT * FROM t WHERE lngUnitNumber=" & _
  13.             lngUnitNumber & " ORDER BY dteDate;"
  14.  
  15.         'get values of first record (earliest date)
  16.         dteTemp = !dteDate
  17.         dblTemp = !dblMWHRS
  18.  
  19.         'move to last record (latest date) and calculate derivative
  20.         .MoveLast
  21.         Derivative = (!dblMWHRS - dblTemp) / DateDiff("d", dteTemp, !dteDate)
  22.         .Close
  23.  
  24.     End With
  25.  
  26.     Set rs = Nothing
  27.  
  28. End Function
  29.  
Query:
Expand|Select|Wrap|Line Numbers
  1. SELECT DISTINCT t.lngUnitNumber, Derivative(t.lngUnitNumber) AS Derivative
  2. FROM t;
  3.  
Regards,
Fish
I'm getting a compliation error "User-defined data type not defined" on this line

rs As New ADODB.Recordset

is ADODB a add in that I may be missing? I'm using Access 97
Aug 28 '08 #12
FishVal
2,653 Recognized Expert Specialist
In VBE window open MainMenu.Tools->References.
Check "Microsoft ActiveX Data Objects x.x Library".

Regards,
Fish
Aug 31 '08 #13

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

Similar topics

6
9194
by: Larry R Harrison Jr | last post by:
I have Access XP, and the following code which is supposed to assign a JPEG to an image control: Me.Image9.Picture = "F:\Pictures\CP775_SonyDSCP50\FingerRockNight_Resize.jpg" I get this error...
6
3910
by: Jean | last post by:
Hi, I am using an Access 2000 front-end to an Oracle 9 backend. I want to write a query that returns all records that are not older than one year for Column "Status_30" (which is a Date). ...
1
2302
by: newtechiebug | last post by:
I am trying to convert a MS Access Query to a SQL query and I need help with some IIF Statements? 2 in particular that I've encountered problems with are: Count(IIf(=0 Or...
3
6159
by: PamelaB | last post by:
I am trying to calculate the year end cost basis of equities held. I have downloaded all the transactions (purchases and sales) for the year and have them in a table. I need to calculate the value...
3
5713
by: gmazza via AccessMonster.com | last post by:
Hi there, I am trying to run a report using a parameter for where the user chooses a month from a combo box. Then on the report, I want it to compare the month to a date field and choose only...
2
7761
by: dvora123 | last post by:
On my form, I need to be able to have Access autocalculate a 3-month followup: This is what I have so far: My field "Web Reviewed Date" and an unbound text box for 3-month follow-up (next...
0
4620
by: SuzK | last post by:
I am trying to calculate in VBA in Access 2002 a moving average and update a table with the calculations. Fields in my WeeklyData table are Week Ending (date) ItemNbr (double) Sales Dollars...
0
1162
by: Anthony Flores | last post by:
My goal is to set Standard Test Times per Unit. There are multiple Operations(10-12 depending on product type) performed during the production of each unit that are being tracked seperately (time...
5
4313
by: neelsfer | last post by:
Its easy to calculate averages in Access queries using the Totals method, but what do you do in the following instance? month-------quantity 1 ------------10 2-------------0 3-------------20...
0
7051
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
7097
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...
1
6750
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
6993
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
5353
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,...
1
4794
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...
0
2993
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1307
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 ...
1
567
muto222
php
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.