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.CountOfName, (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 4 7149
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.CountOfName, (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
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.OpenRecordset("integers", dbOpenTable)
DBEngine(0).BeginTrans
For j = 0 To 9999
rs.AddNew
rs!i = j
rs.Update
Next j
DBEngine(0).CommitTrans
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
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.CountOfName, (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
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.OpenRecordset("integers", dbOpenTable)
DBEngine(0).BeginTrans
For j = 0 To 9999
rs.AddNew
rs!i = j
rs.Update
Next j
DBEngine(0).CommitTrans
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
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.
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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
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)...
|
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...
|
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....
|
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...
|
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...
|
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...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
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...
|
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...
| |