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!
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. -
SELECT
-
((SELECT TOP 1 t.dblMeas FROM t
-
WHERE Year(t.dteDate)=Year(Date()) ORDER BY t.dteDate)+
-
(SELECT TOP 1 t.dblMeas FROM t
-
WHERE Year(t.dteDate)=Year(Date()) ORDER BY t.dteDate DESC))/2 AS Expr1
-
FROM t;
-
Regards,
Fish
Hello.
The same may be done by a single query with two subqueries. -
SELECT
-
((SELECT TOP 1 t.dblMeas FROM t
-
WHERE Year(t.dteDate)=Year(Date()) ORDER BY t.dteDate)+
-
(SELECT TOP 1 t.dblMeas FROM t
-
WHERE Year(t.dteDate)=Year(Date()) ORDER BY t.dteDate DESC))/2 AS Expr1
-
FROM t;
-
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.
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?
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.
"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
FishVal 2,653
Recognized Expert Specialist
Looking to use days.
-
SELECT TOP 1
-
((SELECT TOP 1 t.dblMeas FROM t WHERE Year(t.dteDate)=Year(Date())
-
ORDER BY t.dteDate)+
-
(SELECT TOP 1 t.dblMeas FROM t WHERE Year(t.dteDate)=Year(Date())
-
ORDER BY t.dteDate DESC))/
-
(SELECT DateDiff("d",Min(t.dteDate),Max(t.dteDate)) FROM t
-
WHERE Year(t.dteDate)=Year(Date())) AS Expr1
-
FROM t;
-
More elegant it could be implemented using VBA.
Regards,
Fish
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?
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 - Field; Type; IndexInfo
-
StudentID; AutoNumber; PK
-
Family; String; FK
-
Name; String
-
University; String; FK
-
Mark; Numeric
-
LastAttendance; Date/Time
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 - Field; Type; IndexInfo
-
StudentID; AutoNumber; PK
-
Family; String; FK
-
Name; String
-
University; String; FK
-
Mark; Numeric
-
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.
FishVal 2,653
Recognized Expert Specialist
Ok.
As I've already posted - SQL/VBA hybrid solution will be preferrable.
In code module: -
Public Function Derivative(lngUnitNumber As Long) As Double
-
-
Dim rs As New ADODB.Recordset
-
Dim dteTemp As Date, dblTemp As Double
-
-
With rs
-
-
'opens recordset filtered by particular unit number ordered by date
-
.ActiveConnection = CurrentProject.Connection
-
.CursorType = adOpenDynamic
-
.LockType = adLockOptimistic
-
.Open "SELECT * FROM t WHERE lngUnitNumber=" & _
-
lngUnitNumber & " ORDER BY dteDate;"
-
-
'get values of first record (earliest date)
-
dteTemp = !dteDate
-
dblTemp = !dblMWHRS
-
-
'move to last record (latest date) and calculate derivative
-
.MoveLast
-
Derivative = (!dblMWHRS - dblTemp) / DateDiff("d", dteTemp, !dteDate)
-
.Close
-
-
End With
-
-
Set rs = Nothing
-
-
End Function
-
Query: -
SELECT DISTINCT t.lngUnitNumber, Derivative(t.lngUnitNumber) AS Derivative
-
FROM t;
-
Regards,
Fish
Ok.
As I've already posted - SQL/VBA hybrid solution will be preferrable.
In code module: -
Public Function Derivative(lngUnitNumber As Long) As Double
-
-
Dim rs As New ADODB.Recordset
-
Dim dteTemp As Date, dblTemp As Double
-
-
With rs
-
-
'opens recordset filtered by particular unit number ordered by date
-
.ActiveConnection = CurrentProject.Connection
-
.CursorType = adOpenDynamic
-
.LockType = adLockOptimistic
-
.Open "SELECT * FROM t WHERE lngUnitNumber=" & _
-
lngUnitNumber & " ORDER BY dteDate;"
-
-
'get values of first record (earliest date)
-
dteTemp = !dteDate
-
dblTemp = !dblMWHRS
-
-
'move to last record (latest date) and calculate derivative
-
.MoveLast
-
Derivative = (!dblMWHRS - dblTemp) / DateDiff("d", dteTemp, !dteDate)
-
.Close
-
-
End With
-
-
Set rs = Nothing
-
-
End Function
-
Query: -
SELECT DISTINCT t.lngUnitNumber, Derivative(t.lngUnitNumber) AS Derivative
-
FROM t;
-
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
FishVal 2,653
Recognized Expert Specialist
In VBE window open MainMenu.Tools->References.
Check "Microsoft ActiveX Data Objects x.x Library".
Regards,
Fish
Sign in to post your reply or Sign up for a free account.
Similar topics |
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...
|
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).
...
|
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...
|
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...
|
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...
| |
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...
|
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...
|
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...
|
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...
|
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,...
|
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: 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...
|
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...
|
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: 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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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 ...
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |