Dear Jewel:
Without the SQL of your query so far, and not knowing whether there is a
"grouping" that would reset the total (as opposed to having the running
sum/difference continue to the end of the table) I submit the following
solution as a starting point.
I will use a subquery to find the sum of all preceding rows, and another to
find the value of the preceding row, in order to facilitate what I
understand you want. With a little added complexity, I can modify this to
perform some kind of "grouping" to reset a total, and to give the "NULL"
difference, as you have shown.
SELECT [Date], [Status ID], Inventory
(SELECT SUM(Nz(Inventory, 0))
FROM qry_Inv_byDate Q1
WHERE Q1.[Date] <= Q.[Date])
AS RunningSumOfInventory,
(SELECT Inventory
FROM qry_Inv_byDate Q1
WHERE Q1.[Date] =
(SELECT MAX([Date]
FROM qry_Inv_byDate Q2
WHERE Q2.[Date] < Q.[Date]
AND Q2.[Status ID] = "M"))
AS PreviousInventory
FROM qry_Inv_byDate Q
ORDER BY [Date]
There are potential porblems with this. First, Jet (the default database
engine for Access MDBs) does not usually work with a nested subquery (there
is one in what I wrote) which references the outer query (which I did have
to do). There is a work-around for that, but we'll cross that bridge if
that is truly the case for you. It would complain about a reference to
Q.[Date] if that happens. You could try it temporarily without the
"Previous Inventory" value:
SELECT [Date], [Status ID], Inventory
(SELECT SUM(Nz(Inventory, 0))
FROM qry_Inv_byDate Q1
WHERE Q1.[Date] <= Q.[Date])
AS RunningSumOfInventory
FROM qry_Inv_byDate Q
ORDER BY [Date]
Second, Date would have to be unique for everything to work well. The
difference value would be ambiguous if there are 2 rows for the previous
value (and the query engine will complain of this even if they have the same
value for Inventory). Don't know if that's the case for you. Also, the
running sum will increase simultaneously for all the rows of the same date.
You see, they're not in any order if they have the same date. We can deal
with that problem if it comes up, but be advised, you need to be thinking
about all the data that may be added to the table in the FUTURE, not just
what's there now. Making a query that works now, but not tomorrow, is
rarely a way to get a promotion!
Third, I mentioned the question of "groups" which may reset your totals.
Next, I'm not sure I understand what you mean for the difference. I didn't
give you a difference, but I did give you the PreviousValue, from which a
difference can be readily calculated. However, I had it skip the "M" rows
in doing this. Not sure you want that, or how you want to deal with NULLs.
Please describe what is going on there!
That's not all, I'm sure, but hopefully it would give us a good start. What
do you think?
Tom Ellison
JJEWELL said:
I have a query (qry_Inv_byDate) that has the following records:
Date Status ID Inventory
---------- O -----------
5/1/2001 O 4500
5/9/2001 O 3500
5/10/2001 M Null (or zero if necessary)
5/16/2001 O 3000
5/31/2001 O 2000
I need a field to generate a rolling difference, i.e.,
Diff
-----
Null
1000
3500
-3000
-1000
On another, less urgent, thought, how can I generate the following rolling
total field?
Total
------
4500
8000
8000
11000
13000
I appreciate any help.