I've got a base table with transactions and I need to create a table with running totals. I need them to be per account and and also have a few running totals for each account (depending on the transaction type), and inside that, some running totals per sub-account.
My base table has these fields (more or less) :
AccountID | SubAccountID | TransactionType | TransactionAmount
Considering I've got about 4 types of running totals per Account/TransactionType and 2 more running totals per Account/SubAccount/TransactionType , and I've got about 2M accounts with about 10 sub accounts each, and I'm getting about 10K transactions every minute (at maximum load), how would you do it?
It's also a must that this run asynchronously via an SQL job, creating the aggregations without being part of the transactions themselves.
I'm pretty stuck using a cursor here – which takes way too long. I'd really appreciate any advice / articles that are doing more or less the same.
Best Answer
Asynchronous implies that the running totals do not need to be completely accurate at all times, or your data change patterns are such that a one-off running total build will be valid and accurate until the next load. Anyway, I'm sure you have thought that part through, so I won't labour the point.
Your main options for a high-performance, supported, method are a SQLCLR function/procedure, or an
UPDATE
based on Hugo Kornelis' set-based iteration method. The SQLCLR method (implemented in a procedure, but reasonably easy to translate) can be found here.I haven't been able to find Hugo's method online, but it is detailed in the excellent MVP Deep Dives (Volume 1). Sample code to illustrate Hugo's method (copied from one of my posts on another site you may not have a login for) is shown below:
In SQL Server 2012, you could use the windowing function extensions e.g.
SUM OVER (ORDER BY)
.