> Questions: How do I design the tables or a query so that I can make
> the Begin_Bal change with each entry? How do I compute the number of
> days?
Note that DATE is a reserved word in SQL, so I'll call it BegDate
instead.
You can do what you want using standard subqueries. Break it down into
visible chunks first. You can get the opening balance of the table by
looking at the oldest record:
select First(BegBal) from MyTable order by BegDate ASC)
and you can get the overall change since then just by adding up all the
Change values
select sum(Change) from MyTable as i where i.BegDate <= o.BegDate)
... where o.BegDate is the date you want to go as far as. It is a <=
operator because you need to include the current record each time.
So you can run the whole query like this:
SELECT BegDate,
( SELECT FIRST(BegBal)
FROM MyTable ORDER BY BegDate ASC
) + ( SELECT SUM(Change)
FROM MyTable AS i WHERE i.BegDate <= o.BegDate
) AS EndBal,
BegDate -
( SELECT MAX(BegDate)
FROM MyTable AS d
WHERE d.BegDate < o.BegDate
) AS NumberOfDays
FROM MyTable AS o
ORDER BY o.BegDate ASC;
Hope that helps
Tim F