• Actually, I am using 2012; but I'm not doing a simple running total calculation, I'm doing a weighted average cost calculation, and I have to keep track of three aggregate values that are inter-related but do not always change at the same time:

    QtyOnHand, which is the running total of Qty; This changes on all TranTypes except Verifications (VTI, VC)

    UnitCost, which is an average based on the equation ((Qty_1 * UnitCost_1)..+(Qty_n * UnitCost_n))/SUM(Qty) which changes with Verifications, and may also change on onther incoming TranTypes if the UnitCost is known (sometimes it is, sometimes it isn't)

    ExtendedCost, which is the QtyOnHand * UnitCost (but remember that UnitCost is being calculated at the same time).

    The code I posted above doesn't include my group by, as I'm still working on that one. My parts are grouped by:

    Branch (store branch)

    PartNo (part number)

    SKey (a subset of the part number inventory that is tagged and put aside for an order); Inventory not currently tagged has an SKey of 0.

    And everything is ordered by date.

    The fun part comes in because even though the WAC (Weighted Average Cost) is calculated at the Branch level, sometimes SKeys include more than one branch's running totals - for example, Branch 12 may order parts from Branches 11 and 13 to fill an order, and the unit cost on parts from Branch 11 can be different from the unit cost for parts from Branch 13. So far, so good, but wait - there's more.

    Just to make things interesting, the unit cost of the parts from Branch 13 may change AFTER they were sent, because their original unit cost had not been verified yet. (We receive parts on an ongoing basis from our vendors, and may not know about a price increase until we get the invoice at the end of the month). I'm still working on how to get this change into my calculations in the proper order.