On CBSSports.com: Mike Tyson's daughter dies in accident

Running totals in SQL Server queries

Tags: Databases, Arthur Fuller, current balance, Microsoft SQL Server, withdrawal, SQL Server Newsletter

  • Save
  • Print
  • Recommend
  • 3

Takeaway: Arthur Fuller provides an example of how to run totals in SQL Server queries. Two advantages to such a query include: You don't have to store the results, and you end up with a transaction log that can be inspected row by row.

The requirement is to create a SQL Server query containing running totals based on the moment of the event. The classic example is a bank account because you make deposits and withdrawals, each at a different moment in time. For any given account, one sums the debits (deposits) and credits (withdrawals) at a given point in time. After each transaction, you want to know the current balance. Listing A creates a simple example of such a table.

Here are some sample rows:

1     2006-11-03 02:33:42.34010000.00
2     2006-11-03 02:34:50.467-500.00
3     2006-11-03 02:35:04.857250.00
4     2006-11-03 02:42:19.763-124.25

Since the date is defaulted, all you need to do is add a few amounts. The example keeps it simple, assuming only one bank account (add a column BankAccountNumber for a more realistic scenario).

You are now in a position to create the query that contains the current balance. Since you are recording deposits and withdrawals in the same column as negatives and positives, the sum of the columns is straightforward. To derive the current balance, you sum all the previous transactions and add this sum to the value of the current transaction. The query in Listing B accomplishes this.

This results in the following result set:

1     2006-11-03 02:33:42.34010000.00    10000.00
2     2006-11-03 02:34:50.467-500.00     9500.00
3     2006-11-03 02:35:04.857250.00      9750.00
4     2006-11-03 02:42:19.763-124.25     9625.75

As this example demonstrates, running totals are simple to create, once you understand the requirements. The example presented assumes that the table contains only one account, but it's easy to deal with many 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 thus 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:

  • You don't have to store the results. When scoped by an account number or similar foreign key, performance can be lightning fast.
  • 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.

Miss a tip?

Check out the SQL Server archive, and catch up on the most recently published SQL tips.

TechRepublic's free SQL Server newsletter, delivered each Tuesday, contains hands-on tips that will help you become more adept with this powerful relational database management system. Automatically subscribe today!

  • Save
  • Print
  • Recommend
  • 3

Print/View all Posts Comments on this article

Impractical in reality with large table tequilla@... | 11/07/06
Other Options? hog43 | 11/07/06
re: chapman.tim@... | 11/07/06
Subsets gsquared | 11/07/06
I agree - DO NOT USE THIS TECHNIQUE WITH LARGE TABLES! kbmosher@... | 11/07/06
10000 records gsquared | 11/08/06
Scalability artful@... | 11/10/06
Does SQL Server have anything like the Oracle LAG() function? Mannion | 11/08/06
LAG() artful@... | 11/10/06
RowID() function hsp@... | 11/22/07
Misinformation DBAdmin | 11/07/06
'Transaction log' in the colloquial sense Mannion | 11/07/06
Maybe, but... DBAdmin | 11/07/06
Transaction Logs artful@... | 11/10/06

What do you think?

White Papers, Webcasts, and Downloads

Article Categories

Security
Security Solutions, IT Locksmith
Networking and Communications
E-mail Administration NetNote, Cisco Routers and Switches
CIO and IT Management
Project Management, CIO Issues, Strategies that Scale
Desktops, Laptops & OS
Windows 2000 Professional, Microsoft Word, Microsoft Excel, Microsoft Access, Windows XP,
Data Management
Oracle, SQL Server
Servers
Windows NT, Linux NetNote, Windows Server 2003
Career Development
Geek Trivia
Software/Web Development
Web Development Zone, Visual Basic, .NET

SmartPlanet

advertisement
Click Here