A Microsoft Office (Excel, Word) forum. OfficeFrustration

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

Go Back   Home » OfficeFrustration forum » Microsoft Access » Running & Setting Up Queries
Site Map Home Register Authors List Search Today's Posts Mark Forums Read  

12 Month Running Total



 
 
Thread Tools Display Modes
  #1  
Old March 8th, 2010, 11:16 PM posted to microsoft.public.access.queries
tighe
external usenet poster
 
Posts: 53
Default 12 Month Running Total

All,

thanks for the advice to get me this far. previously i had asked about a 12
month rolling total, below is the SQl but for Decembers the total is wrong,
can anyone suggest a fix?
AC2007/Xp
thanks in advance.

SELECT Firm_Employee_Count_time.Financial_Month,
Format([Financial_Month],'yyyymm') AS FinMon,
Firm_Employee_Count_time.OccurDate,
[Firm_Employee_Count_time]![OccurDate]-IIf(Right([Firm_Employee_Count_time]![OccurDate],2)=12,111,99)
AS OccurPrevious, Firm_Employee_Count_time.EmployeeCount,
Nz([Firm_Employee_Count_Fired]![EmployeeCount],0)*-1 AS FiredNum,
Nz([Firm_Employee_Count_Hired]![EmployeeCount],0) AS HiredNum,
[FiredNum]+[HiredNum] AS Net,
DSum("Net","Firm_Count_Hired_Fired","Format([Financial_Month], 'yyyymm')=" &
[Firm_Employee_Count_time]![OccurDate] & " And Format([Financial_Month],
'yyyymm')=" & [OccurPrevious] & "") AS RunTot
FROM (Firm_Employee_Count_time LEFT JOIN Firm_Employee_Count_Fired ON
Firm_Employee_Count_time.OccurDate = Firm_Employee_Count_Fired.OccurDate)
LEFT JOIN Firm_Employee_Count_Hired ON Firm_Employee_Count_time.OccurDate =
Firm_Employee_Count_Hired.OccurDate
GROUP BY Firm_Employee_Count_time.Financial_Month,
Format([Financial_Month],'yyyymm'), Firm_Employee_Count_time.OccurDate,
[Firm_Employee_Count_time]![OccurDate]-IIf(Right([Firm_Employee_Count_time]![OccurDate],2)=12,111,99),
Firm_Employee_Count_time.EmployeeCount,
Nz([Firm_Employee_Count_Fired]![EmployeeCount],0)*-1,
Nz([Firm_Employee_Count_Hired]![EmployeeCount],0);

  #2  
Old March 9th, 2010, 10:20 PM posted to microsoft.public.access.queries
KARL DEWEY
external usenet poster
 
Posts: 10,767
Default 12 Month Running Total

You are not summing anything in the query that I can see but I do see other
things that are questionable.
Why both Firm_Employee_Count_time.Financial_Month and
Format([Financial_Month],'yyyymm'
What is the datatype of Financial_Month?

You are atempting to create and use an alias in the same query --
Nz([Firm_Employee_Count_Hired]![EmployeeCount],0) AS HiredNum,
[FiredNum]+[HiredNum] AS Net, [HiredNum]
In most cases it will not work. You have to use the same mathmatical
formula instead of the alias.

What is [OccurDate] and why are you subtracting numbers from it?
[Firm_Employee_Count_time]![OccurDate]-IIf(Right([Firm_Employee_Count_time]![OccurDate],2)=12,111,99) AS OccurPrevious,

And then you atempt to use the alias, [OccurPrevious], in your DSum.

--
Build a little, test a little.


"tighe" wrote:

All,

thanks for the advice to get me this far. previously i had asked about a 12
month rolling total, below is the SQl but for Decembers the total is wrong,
can anyone suggest a fix?
AC2007/Xp
thanks in advance.

SELECT Firm_Employee_Count_time.Financial_Month,
Format([Financial_Month],'yyyymm') AS FinMon,
Firm_Employee_Count_time.OccurDate,
[Firm_Employee_Count_time]![OccurDate]-IIf(Right([Firm_Employee_Count_time]![OccurDate],2)=12,111,99)
AS OccurPrevious, Firm_Employee_Count_time.EmployeeCount,
Nz([Firm_Employee_Count_Fired]![EmployeeCount],0)*-1 AS FiredNum,
Nz([Firm_Employee_Count_Hired]![EmployeeCount],0) AS HiredNum,
[FiredNum]+[HiredNum] AS Net,
DSum("Net","Firm_Count_Hired_Fired","Format([Financial_Month], 'yyyymm')=" &
[Firm_Employee_Count_time]![OccurDate] & " And Format([Financial_Month],
'yyyymm')=" & [OccurPrevious] & "") AS RunTot
FROM (Firm_Employee_Count_time LEFT JOIN Firm_Employee_Count_Fired ON
Firm_Employee_Count_time.OccurDate = Firm_Employee_Count_Fired.OccurDate)
LEFT JOIN Firm_Employee_Count_Hired ON Firm_Employee_Count_time.OccurDate =
Firm_Employee_Count_Hired.OccurDate
GROUP BY Firm_Employee_Count_time.Financial_Month,
Format([Financial_Month],'yyyymm'), Firm_Employee_Count_time.OccurDate,
[Firm_Employee_Count_time]![OccurDate]-IIf(Right([Firm_Employee_Count_time]![OccurDate],2)=12,111,99),
Firm_Employee_Count_time.EmployeeCount,
Nz([Firm_Employee_Count_Fired]![EmployeeCount],0)*-1,
Nz([Firm_Employee_Count_Hired]![EmployeeCount],0);

  #3  
Old March 10th, 2010, 07:21 PM posted to microsoft.public.access.queries
tighe
external usenet poster
 
Posts: 53
Default 12 Month Running Total

Karl,

the datatype of Financial_Month is date but the queries i have the dates are
in format "yyyymm", i dont really need to select
Firm_Employee_Count_time.Financial_Month.

useing the alias works fine in the calculation in this case

since all the OccurDate is formatted as "yyyymm" they are not longer dates
so to get 12 months previous that is the caluclation to get the proper
result. this was the only way to get a rolling 12 month instead of the same
year as in posting and KB i found.

i realize that most of this is a bit screwy but so far it works except for
the Runtot for Decembers, which is what i need help with. i updated the
query to your suggestions:

SELECT Firm_Employee_Count_time.OccurDate,
Firm_Employee_Count_time.EmployeeCount,
Nz([Firm_Employee_Count_Fired]![EmployeeCount],0)*-1 AS FiredNum,
Nz([Firm_Employee_Count_Hired]![EmployeeCount],0) AS HiredNum,
Nz([Firm_Employee_Count_Fired]![EmployeeCount],0)*-1+Nz([Firm_Employee_Count_Hired]![EmployeeCount],0)
AS Net, DSum("Net","Firm_Count_Hired_Fired","Format([Financial_Month],
'yyyymm')=" & [Firm_Employee_Count_time]![OccurDate] & " And
Format([Financial_Month], 'yyyymm')=" &
[Firm_Employee_Count_time]![OccurDate]-IIf(Right([Firm_Employee_Count_time]![OccurDate],2)=12,111,99) & "") AS RunTot
FROM (Firm_Employee_Count_time LEFT JOIN Firm_Employee_Count_Fired ON
Firm_Employee_Count_time.OccurDate = Firm_Employee_Count_Fired.OccurDate)
LEFT JOIN Firm_Employee_Count_Hired ON Firm_Employee_Count_time.OccurDate =
Firm_Employee_Count_Hired.OccurDate
GROUP BY Firm_Employee_Count_time.OccurDate,
Firm_Employee_Count_time.EmployeeCount,
Nz([Firm_Employee_Count_Fired]![EmployeeCount],0)*-1,
Nz([Firm_Employee_Count_Hired]![EmployeeCount],0);


"KARL DEWEY" wrote:

You are not summing anything in the query that I can see but I do see other
things that are questionable.
Why both Firm_Employee_Count_time.Financial_Month and
Format([Financial_Month],'yyyymm'
What is the datatype of Financial_Month?

You are atempting to create and use an alias in the same query --
Nz([Firm_Employee_Count_Hired]![EmployeeCount],0) AS HiredNum,
[FiredNum]+[HiredNum] AS Net, [HiredNum]
In most cases it will not work. You have to use the same mathmatical
formula instead of the alias.

What is [OccurDate] and why are you subtracting numbers from it?
[Firm_Employee_Count_time]![OccurDate]-IIf(Right([Firm_Employee_Count_time]![OccurDate],2)=12,111,99) AS OccurPrevious,

And then you atempt to use the alias, [OccurPrevious], in your DSum.

--
Build a little, test a little.


"tighe" wrote:

All,

thanks for the advice to get me this far. previously i had asked about a 12
month rolling total, below is the SQl but for Decembers the total is wrong,
can anyone suggest a fix?
AC2007/Xp
thanks in advance.

SELECT Firm_Employee_Count_time.Financial_Month,
Format([Financial_Month],'yyyymm') AS FinMon,
Firm_Employee_Count_time.OccurDate,
[Firm_Employee_Count_time]![OccurDate]-IIf(Right([Firm_Employee_Count_time]![OccurDate],2)=12,111,99)
AS OccurPrevious, Firm_Employee_Count_time.EmployeeCount,
Nz([Firm_Employee_Count_Fired]![EmployeeCount],0)*-1 AS FiredNum,
Nz([Firm_Employee_Count_Hired]![EmployeeCount],0) AS HiredNum,
[FiredNum]+[HiredNum] AS Net,
DSum("Net","Firm_Count_Hired_Fired","Format([Financial_Month], 'yyyymm')=" &
[Firm_Employee_Count_time]![OccurDate] & " And Format([Financial_Month],
'yyyymm')=" & [OccurPrevious] & "") AS RunTot
FROM (Firm_Employee_Count_time LEFT JOIN Firm_Employee_Count_Fired ON
Firm_Employee_Count_time.OccurDate = Firm_Employee_Count_Fired.OccurDate)
LEFT JOIN Firm_Employee_Count_Hired ON Firm_Employee_Count_time.OccurDate =
Firm_Employee_Count_Hired.OccurDate
GROUP BY Firm_Employee_Count_time.Financial_Month,
Format([Financial_Month],'yyyymm'), Firm_Employee_Count_time.OccurDate,
[Firm_Employee_Count_time]![OccurDate]-IIf(Right([Firm_Employee_Count_time]![OccurDate],2)=12,111,99),
Firm_Employee_Count_time.EmployeeCount,
Nz([Firm_Employee_Count_Fired]![EmployeeCount],0)*-1,
Nz([Firm_Employee_Count_Hired]![EmployeeCount],0);

  #4  
Old March 10th, 2010, 11:43 PM posted to microsoft.public.access.queries
KARL DEWEY
external usenet poster
 
Posts: 10,767
Default 12 Month Running Total

You may need to tweak the Format(DateAdd("m", -12,[Financial_Month]),
'yyyymm') a little to get the full month ---
SELECT Firm_Employee_Count_time.OccurDate,
Firm_Employee_Count_time.EmployeeCount,
Nz([Firm_Employee_Count_Fired]![EmployeeCount],0)*-1 AS FiredNum,
Nz([Firm_Employee_Count_Hired]![EmployeeCount],0) AS HiredNum,
Nz([Firm_Employee_Count_Fired]![EmployeeCount],0)*-1+N([Firm_Employee_Count_Hired]![EmployeeCount],0)
AS Net, (SELECT Nz([YY].[EmployeeCount],0)*- 1+Nz([ZZ].[EmployeeCount],0)
FROM(Firm_Employee_Count_time AS [XX] LEFT JOIN Firm_Employee_Count_Fired
AS[YY] ON [XX].OccurDate = [YY].OccurDate) LEFT JOIN
Firm_Employee_Count_Hired AS [ZZ] ON [XX].OccurDate = [ZZ].OccurDate WHERE
([YY].OccurDate Between Format
([Financial_Month], 'yyyymm') AND Format(DateAdd("m",
-12,[Financial_Month]), 'yyyymm')) AND ([ZZ].OccurDate Between
Format([Financial_Month], 'yyyymm') AND Format(DateAdd("m",
-12,[Financial_Month]), 'yyyymm'))) AS RunTot
FROM (Firm_Employee_Count_time LEFT JOIN Firm_Employee_Count_Fired ON
Firm_Employee_Count_time.OccurDate = Firm_Employee_Count_Fired.OccurDate)
LEFT JOIN Firm_Employee_Count_Hired ON Firm_Employee_Count_time.OccurDate =
Firm_Employee_Count_Hired.OccurDate
GROUP BY Firm_Employee_Count_time.OccurDate,
Firm_Employee_Count_time.EmployeeCount,
Nz([Firm_Employee_Count_Fired]![EmployeeCount],0)*-1,
Nz([Firm_Employee_Count_Hired]![EmployeeCount],0);

--
Build a little, test a little.


"tighe" wrote:

Karl,

the datatype of Financial_Month is date but the queries i have the dates are
in format "yyyymm", i dont really need to select
Firm_Employee_Count_time.Financial_Month.

useing the alias works fine in the calculation in this case

since all the OccurDate is formatted as "yyyymm" they are not longer dates
so to get 12 months previous that is the caluclation to get the proper
result. this was the only way to get a rolling 12 month instead of the same
year as in posting and KB i found.

i realize that most of this is a bit screwy but so far it works except for
the Runtot for Decembers, which is what i need help with. i updated the
query to your suggestions:

SELECT Firm_Employee_Count_time.OccurDate,
Firm_Employee_Count_time.EmployeeCount,
Nz([Firm_Employee_Count_Fired]![EmployeeCount],0)*-1 AS FiredNum,
Nz([Firm_Employee_Count_Hired]![EmployeeCount],0) AS HiredNum,
Nz([Firm_Employee_Count_Fired]![EmployeeCount],0)*-1+Nz([Firm_Employee_Count_Hired]![EmployeeCount],0)
AS Net, DSum("Net","Firm_Count_Hired_Fired","Format([Financial_Month],
'yyyymm')=" & [Firm_Employee_Count_time]![OccurDate] & " And
Format([Financial_Month], 'yyyymm')=" &
[Firm_Employee_Count_time]![OccurDate]-IIf(Right([Firm_Employee_Count_time]![OccurDate],2)=12,111,99) & "") AS RunTot
FROM (Firm_Employee_Count_time LEFT JOIN Firm_Employee_Count_Fired ON
Firm_Employee_Count_time.OccurDate = Firm_Employee_Count_Fired.OccurDate)
LEFT JOIN Firm_Employee_Count_Hired ON Firm_Employee_Count_time.OccurDate =
Firm_Employee_Count_Hired.OccurDate
GROUP BY Firm_Employee_Count_time.OccurDate,
Firm_Employee_Count_time.EmployeeCount,
Nz([Firm_Employee_Count_Fired]![EmployeeCount],0)*-1,
Nz([Firm_Employee_Count_Hired]![EmployeeCount],0);


"KARL DEWEY" wrote:

You are not summing anything in the query that I can see but I do see other
things that are questionable.
Why both Firm_Employee_Count_time.Financial_Month and
Format([Financial_Month],'yyyymm'
What is the datatype of Financial_Month?

You are atempting to create and use an alias in the same query --
Nz([Firm_Employee_Count_Hired]![EmployeeCount],0) AS HiredNum,
[FiredNum]+[HiredNum] AS Net, [HiredNum]
In most cases it will not work. You have to use the same mathmatical
formula instead of the alias.

What is [OccurDate] and why are you subtracting numbers from it?
[Firm_Employee_Count_time]![OccurDate]-IIf(Right([Firm_Employee_Count_time]![OccurDate],2)=12,111,99) AS OccurPrevious,

And then you atempt to use the alias, [OccurPrevious], in your DSum.

--
Build a little, test a little.


"tighe" wrote:

All,

thanks for the advice to get me this far. previously i had asked about a 12
month rolling total, below is the SQl but for Decembers the total is wrong,
can anyone suggest a fix?
AC2007/Xp
thanks in advance.

SELECT Firm_Employee_Count_time.Financial_Month,
Format([Financial_Month],'yyyymm') AS FinMon,
Firm_Employee_Count_time.OccurDate,
[Firm_Employee_Count_time]![OccurDate]-IIf(Right([Firm_Employee_Count_time]![OccurDate],2)=12,111,99)
AS OccurPrevious, Firm_Employee_Count_time.EmployeeCount,
Nz([Firm_Employee_Count_Fired]![EmployeeCount],0)*-1 AS FiredNum,
Nz([Firm_Employee_Count_Hired]![EmployeeCount],0) AS HiredNum,
[FiredNum]+[HiredNum] AS Net,
DSum("Net","Firm_Count_Hired_Fired","Format([Financial_Month], 'yyyymm')=" &
[Firm_Employee_Count_time]![OccurDate] & " And Format([Financial_Month],
'yyyymm')=" & [OccurPrevious] & "") AS RunTot
FROM (Firm_Employee_Count_time LEFT JOIN Firm_Employee_Count_Fired ON
Firm_Employee_Count_time.OccurDate = Firm_Employee_Count_Fired.OccurDate)
LEFT JOIN Firm_Employee_Count_Hired ON Firm_Employee_Count_time.OccurDate =
Firm_Employee_Count_Hired.OccurDate
GROUP BY Firm_Employee_Count_time.Financial_Month,
Format([Financial_Month],'yyyymm'), Firm_Employee_Count_time.OccurDate,
[Firm_Employee_Count_time]![OccurDate]-IIf(Right([Firm_Employee_Count_time]![OccurDate],2)=12,111,99),
Firm_Employee_Count_time.EmployeeCount,
Nz([Firm_Employee_Count_Fired]![EmployeeCount],0)*-1,
Nz([Firm_Employee_Count_Hired]![EmployeeCount],0);

 




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump


All times are GMT +1. The time now is 09:35 AM.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 OfficeFrustration.
The comments are property of their posters.