Tom,
I am running a selective running sum on an inventroy
database. Therefore, I have an "on-hand" which needs to
be reduced by consumption for each item.
Here's the SQL I've been playing with. My table name is
wwwMASTER for WO sort. NewInv is just a copy of Inventory
SELECT [wwwMASTER for WO sort].ItemProductCode, [wwwMASTER
for WO sort].ItemProductCode AS IPCalias, [wwwMASTER for
WO sort].ProductName, [wwwMASTER for WO sort].Source,
[wwwMASTER for WO sort].Inventory, [wwwMASTER for WO
sort].Inventory AS NewInv, DSum([Inventory],"wwwMASTER for
WO sort",[ItemProductCode]=[IPCalias]) AS InvSoFar,
[wwwMASTER for WO sort].InventoryUnit, [wwwMASTER for WO
sort].OrderDesc, [wwwMASTER for WO sort].CustomerID,
[wwwMASTER for WO sort].CustomerPO, [wwwMASTER for WO
sort].ProductCode
FROM [wwwMASTER for WO sort]
GROUP BY [wwwMASTER for WO sort].ItemProductCode,
[wwwMASTER for WO sort].ItemProductCode, [wwwMASTER for WO
sort].ProductName, [wwwMASTER for WO sort].Source,
[wwwMASTER for WO sort].Inventory, [wwwMASTER for WO
sort].Inventory, [wwwMASTER for WO sort].InventoryUnit,
[wwwMASTER for WO sort].OrderDesc, [wwwMASTER for WO
sort].CustomerID, [wwwMASTER for WO sort].CustomerPO,
[wwwMASTER for WO sort].ProductCode
ORDER BY [wwwMASTER for WO sort].ItemProductCode,
[wwwMASTER for WO sort].Source;
This is close. The I'm using inventory to calculate
InvSoFar, but the calulation is giving me some strange #'s.
Bill
-----Original Message-----
Dear Bill:
A running sum can be performed in a query using a
subquery. This must
be based on a sorted order of the rows in that query.
The running sum
can be over all the rows in the query, or it can start
over in a
"group", being defined as subsets of rows in which one or
more columns
do not change.
For a simple case of a running sum over an entire query
resutls set:
SELECT Sort1, (SELECT SUM(Value) FROM YourTable T2
WHERE T2.Sort1 <= T1.Sort1) AS RunningSum
FROM YourTable T1
ORDER BY Sort1
T1 and T2 are aliasses for two independent references to
the same
table.
If you need a more complex running sum I could help with
it here.
Please post a query of your situation, omitting the
running sum. It
must include an ORDER BY clause putting the rows in the
sequence you
want them summed. If you want the sum to restart based
on grouping,
please state the columns on which it is to be grouped.
These columns
also should be the first columns of the ORDER BY clause.
From that I can likely create the running sum subquery
necessary.
One note on the ordering. If it is not unique, the
subquery will add
the new values for all the rows where the ordering is
identical
simultaneously. The query will present these rows one at
a time, but
the running sum will not be able to distinguish between
them. If you
add one or more additional key fields till the ordering
is unique this
can be eliminated. When the set of ordering fields is
not unique, the
ordering among rows having duplicate ordering is
arbitrary, causing
this potential problem.
In large databases this can be a bit slow in some cases.
On Sun, 28 Sep 2003 07:44:13 -0700, "Bill"
<
[email protected]>
wrote:
Is ther any way to do a running sum in a query? I can,
of
course, do this in a report, but I would like to do this
as part of an inventory check in a table or query.
Thanks to anyone who solves this for me,
Bill
Tom Ellison
Microsoft Access MVP
Ellison Enterprises - Your One Stop IT Experts
.