|You are asked to write a query that returns time-based running totals. The classic example is a bank account.|
For any given account, you sum the debits (deposits) and credit (withdrawals) up to that time. After each transaction, you want to know the current balance. Here are sample rows:
Since you are recording deposits and withdrawals in the same column, as negatives and positives, the sum is straightforward. To derive the current balance, you sum all the previous transactions and add this sum to the value of the current transaction:
Here is the result:
We assumed one account. To handle multiple accounts, add a column for BankAccountNumber and a WHERE predicate that specifies the account of interest.
You can also turn this example upside down and create a running difference (as in an inventory count). You begin with an inventory of 1,000, and then subtract various purchases and receipts.
There are two advantages to such a query:
1. You don't have to store the results. When scoped by an account number or similar foreign key, performance can be lightning fast.
2. You end up with a transaction log that can be inspected row-by-row. If a bug turns up, you will be able to isolate the particular transaction that caused it.