12 Month Running Total

T

tighe

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);
 
K

KARL DEWEY

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.
 
T

tighe

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 said:
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 said:
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);
 
K

KARL DEWEY

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 said:
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 said:
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 said:
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);
 
Top