Your expected results seem to involve the prior month (May), not the
beginning of the quarter (April). The beginning of quarter containing June
is April which seems to be Zero from what I can tell from your posting.
SELECT T.*
, TT.AmountYTD -
(SELECT TT.amountYTD AS TQtrDiff
FROM leasings AS TT
WHERE Month(TT.[Month]) =
Month(T.[Month])+(Choose((Month(T.[Month]) Mod 3)+1,-2,0,-1))) AS QtrDiff
FROM leasings AS T
ORDER BY T.Month;
You might want to set up the query (temporarily) as the following so you can
trouble shoot.
SELECT T.*
, T.AmountYTD - (SELECT TT.amountYTD AS TQtrDiff
FROM leasings AS TT
WHERE Month(TT.[Month]) =
Month(T.[Month])+(Choose((Month(T.[Month]) Mod 3)+1,-2,0,-1))) AS qtrDiff
, (SELECT TT.amountYTD AS TQtrDiff
FROM leasings AS TT
WHERE Month(TT.[Month]) =
Month(T.[Month])+(Choose((Month(T.[Month]) Mod 3)+1,-2,0,-1))) AS
QuarterStartValue
FROM leasings AS T
ORDER BY T.Month;
Gwen H said:
Here's my SQL:
SELECT T.*, (SELECT (TT.amountYTD-T.amountYTD) AS TQtrDiff
FROM leasings AS TT
WHERE Month(TT.[Month]) =
Month(T.[Month])+(Choose((Month(T.[Month]) Mod 3)+1,-2,0,-1))) AS QtrDiff
FROM leasings AS T
ORDER BY T.Month;
And here's the result I'm getting:
May ($2,541,810.00) - this is correct
June ($6,091,231.00) - this needs to be $3,549,421
Thanks!
Ken Snell (MVP) said:
The query's SQL statement that I posted should give you what you seek,
assuming that you have the YTD field in the original table or query.
How about you post the exact query SQL that you tried, and the output
results from that query, so that we can see what you mean by "kinda
works"?
--
Ken Snell
<MS ACCESS MVP>
Thanks for your response ... it kinda works, but not quite. Here are
the
actual amounts for 2006 - this might help you understand my problem a
bit
better.
Jan thru Feb - $0 each month
May - $2,541,810
June - $6,091,231
July thru October - $0 each month
So for the month of May I need the query to tell me that May's total
was
$2,541,810, and for June I need the query to tell me that June's total
was
$3,549,421. Does that help?
Also, I do not need the query to span multiple years. In January of
each
year, we archive the current database with all its data intact, then
using
another copy of the database we delete all the data and start fresh. So
in
January of each year I need the query to concentrate only on the
previous
year, but in February we will be focusing solely on the current year.
I hope I'm making sense!
:
SELECT T.*,
(SELECT (TT.YTD-T.YTD) AS TQtrDiff
FROM TableName AS TT
WHERE Month(TT.[Month]) =
Month(T.[Month])+(Choose((Month(T.[Month]) Mod 3)+1,-2,0,-1)))
AS QtrDiff
FROM TableName AS T;
--
Ken Snell
<MS ACCESS MVP>
I have a table that gets a new YTD entry once a month. Sample data:
Officer# OfficerName Month YTD
100 Jane Doe 7/31/06 $6,091,022
100 Jane Doe 8/31/06 $6,091,023
100 Jane Doe 9/30/06 $6,091,024
I need a query that will take the most recent month's amount and
subtract
the first month of the quarter from it. In other words, using the
sample
data
above, a query that subtracts July's amount from September's. And, I
need
four of these queries - one for each quarter (unless it can be done
in
one
query).
Many thanks for any ideas and pseudocode you are willing to share.