473,795 Members | 2,805 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Rolling Average Calculation Problem

I have a table with the following fields - Location, Manager,
CostCentre, Month and Headcount. What I am trying to do is a monthly
rolling average headcount by Location, Manager, CostCentre. To verify
the calculations I am using sum rather then average and the query below
is showing the sum of all records in the month rather then breaking it
down by the Location, Manager, CostCentre combinations.

SELECT test.Loc, test.[Line Mgr], test.CC, test.Month,
test.CountOfNam e, (SELECT round(sum(Dupe. CountOfName),0) AS
AvgCountOfName FROM [Test] AS Dupe WHERE Loc=Dupe.Loc and [Line
Mgr]=dupe.[line mgr] and cc=dupe.cc and Dupe.Month Between DateAdd("m",
-3, [test].[Month]) And [test].[Month]) AS RollingSum
FROM test
ORDER BY test.Loc, test.[Line Mgr], test.CC, test.Month;

Any help will be greatly appreciated

Nov 13 '05 #1
4 7182
On 31 Jul 2005 17:28:43 -0700, da************@ bigpond.com wrote:
I have a table with the following fields - Location, Manager,
CostCentre, Month and Headcount. What I am trying to do is a monthly
rolling average headcount by Location, Manager, CostCentre. To verify
the calculations I am using sum rather then average and the query below
is showing the sum of all records in the month rather then breaking it
down by the Location, Manager, CostCentre combinations.

SELECT test.Loc, test.[Line Mgr], test.CC, test.Month,
test.CountOfNa me, (SELECT round(sum(Dupe. CountOfName),0) AS
AvgCountOfNa me FROM [Test] AS Dupe WHERE Loc=Dupe.Loc and [Line
Mgr]=dupe.[line mgr] and cc=dupe.cc and Dupe.Month Between DateAdd("m",
-3, [test].[Month]) And [test].[Month]) AS RollingSum
FROM test
ORDER BY test.Loc, test.[Line Mgr], test.CC, test.Month;

Any help will be greatly appreciated

Hi
Moving averages and sums, if required in SQL, are usually done using proprietary
features, eg Oracle has a number of special features for this sort of thing.

Here is a way you can use in SQL which uses a supplementary table of integers,
which is my pet way of doing things. Create the table by pasting this code
in a module, clicking inside it and pressing F5:

Public Sub VSetup()
Dim rs As Recordset
Dim j As Long
Dim db As Database
On Error Resume Next ' (!)
Set db = DBEngine(0)(0)
db.Execute "CREATE TABLE integers (i INTEGER );"
Set rs = db.OpenRecordse t("integers", dbOpenTable)
DBEngine(0).Beg inTrans
For j = 0 To 9999
rs.AddNew
rs!i = j
rs.Update
Next j
DBEngine(0).Com mitTrans
rs.Close
db.Execute "CREATE INDEX PrimaryKey ON integers (i) WITH PRIMARY;"
End Sub

10,000 is usually enough! (These are SQL integers, ie vba long)

Now, assume that all your months are represented by a single number - eg
you could use the mapping generated by a stored query "Vmonths"

SELECT DISTINCTROW DateAdd("m",[i],CDate("1 January 1990")) AS DateValue, Integers.i
FROM Integers
ORDER BY Integers.i;

which generates months from January 1990 to April 2823.

Then the query

SELECT test.Loc, test.[Line mgr], test.CC, test.Month AS MonthFrom, [Month]+2 AS MonthTo, Count(test.Name ) AS CountOfName
FROM test INNER JOIN integers ON test.Month = integers.i
WHERE (((test.Month) Between [i] And [i]+3))
GROUP BY test.Loc, test.[Line mgr], test.CC, test.Month, [Month]+2;

should give you the name count for every three month period.
but be warned I haven't tested it properly as I haven't much data!

HTH
David
Nov 13 '05 #2
On 31 Jul 2005 17:28:43 -0700, da************@ bigpond.com wrote:
I have a table with the following fields - Location, Manager,
CostCentre, Month and Headcount. What I am trying to do is a monthly
rolling average headcount by Location, Manager, CostCentre. To verify
the calculations I am using sum rather then average and the query below
is showing the sum of all records in the month rather then breaking it
down by the Location, Manager, CostCentre combinations.

SELECT test.Loc, test.[Line Mgr], test.CC, test.Month,
test.CountOfNa me, (SELECT round(sum(Dupe. CountOfName),0) AS
AvgCountOfNa me FROM [Test] AS Dupe WHERE Loc=Dupe.Loc and [Line
Mgr]=dupe.[line mgr] and cc=dupe.cc and Dupe.Month Between DateAdd("m",
-3, [test].[Month]) And [test].[Month]) AS RollingSum
FROM test
ORDER BY test.Loc, test.[Line Mgr], test.CC, test.Month;

Any help will be greatly appreciated

Hi
Moving averages and sums, if required in SQL, are usually done using proprietary
features, eg Oracle has a number of special features for this sort of thing.

Here is a way you can use in SQL which uses a supplementary table of integers,
which is my pet way of doing things. Create the table by pasting this code
in a module, clicking inside it and pressing F5:

Public Sub VSetup()
Dim rs As Recordset
Dim j As Long
Dim db As Database
On Error Resume Next ' (!)
Set db = DBEngine(0)(0)
db.Execute "CREATE TABLE integers (i INTEGER );"
Set rs = db.OpenRecordse t("integers", dbOpenTable)
DBEngine(0).Beg inTrans
For j = 0 To 9999
rs.AddNew
rs!i = j
rs.Update
Next j
DBEngine(0).Com mitTrans
rs.Close
db.Execute "CREATE INDEX PrimaryKey ON integers (i) WITH PRIMARY;"
End Sub

10,000 is usually enough! (These are SQL integers, ie vba long)

Now, assume that all your months are represented by a single number - eg
you could use the mapping generated by a stored query "Vmonths"

SELECT DISTINCTROW DateAdd("m",[i],CDate("1 January 1990")) AS DateValue, Integers.i
FROM Integers
ORDER BY Integers.i;

which generates months from January 1990 to April 2823.

Then the query

SELECT test.Loc, test.[Line mgr], test.CC, test.Month AS MonthFrom, [Month]+2 AS MonthTo, Count(test.Name ) AS CountOfName
FROM test INNER JOIN integers ON test.Month = integers.i
WHERE (((test.Month) Between [i] And [i]+3))
GROUP BY test.Loc, test.[Line mgr], test.CC, test.Month, [Month]+2;

should give you the name count for every three month period.
but be warned I haven't tested it properly as I haven't much data!

HTH
David
Nov 13 '05 #3
P.S.
That assumes you want a value once each month for the three month rolling sum, if you want a value each day
(for example) that can be done in a similar way.

Nov 13 '05 #4
P.S.
That assumes you want a value once each month for the three month rolling sum, if you want a value each day
(for example) that can be done in a similar way.

Nov 13 '05 #5

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

Similar topics

2
13608
by: Wayne Aprato | last post by:
I've read most, if not all, of the posts on moving average and still can't find a simple solution to my problem (if a simple solution exists!) I have a table with 2 fields: Hours and Injuries. I have a query based on this table which shows these 2 fields and calculates a third field: Frequency Rate, based on a formula which uses the Hours and Injuries fields. Is there a simple way of A: using yet another calculated field in the query...
4
10873
by: CliffKing | last post by:
Hi, Is there a way i can compute a moving/rolling average in ms access? I have this excel spreadsheet (see below) and i would like to automate it in ms access %Change = (C5-D5)/D5 4WKAvg = SUM(E2:E5)/4
1
1944
by: Joost | last post by:
Who can help me, I have a table which contain an artikels and a prices. What i want is to the following 1) The average price from the last 7 records. 2) The average from the last 14 records 3) Divide the 7 day average with the 14 day average. Who can help me.
3
9041
by: Frank Rizzo | last post by:
I am trying to do some monitoring of some PerfMon counters and have a question. Does PerfMon figure out the Minimum, Maximum, Average values for each counter? Or are those values part of the performance monitoring subsystem and can be accessed via the System.Diagnostics.PerformanceCounter object. I haven't found an obvious way to get to the Minimum, Maximum, Average values for a counter. Thanks.
3
5309
by: Salad | last post by:
http://www.mathwords.com/w/weighted_average.htm At the above link gives an example of a weighted average. It uses the following example: Grades are often computed using a weighted average. Suppose that homework counts 10%, quizzes 20%, and tests 70%. If Pat has a homework grade of 92, a quiz grade of 68, and a test grade of 81, then
1
1835
by: donb01 | last post by:
I want to create a rolling average X of the last 5 7day1's on a report, I don't know how to do it either in a query or report. Can someone help me here? 7 Day1 5pt avg SF227 3870 2160 3580 3510 2990 X 2800 X
5
4594
by: jamesnkk | last post by:
I want to calculate the average price, I have a cost price table- tblcostPrice as below,Same part no can be purchase from different supplier at different cost. In my tblcostPrice I have 2 suppliers for the same part at different cost Part No - KN-12345 Product ID....SUPPLIER......Qty......Cost price A0123.........AAA CO.......1000..... 0.90 A0123.........AAA CO...... 5000..... 0.70 A0123.........AAA CO.......9000..... 0.55
13
4906
by: vaneric | last post by:
hi i have a set of RGB images of diff faces (of people )as a 2 dim numpyarray ...something like threefaces=array(, , ]) where xa1,xa2,xa3 are tuples each representing rgb values of a pixel of first image ..
12
9148
by: denveromlp | last post by:
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...
0
9673
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9522
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10443
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
10165
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,...
1
7543
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
5437
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...
1
4113
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
2
3728
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2921
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.