Tuesday, April 1, 2008

SQL for Running Totals: The Power of Cross Join


SELECT
t.transaction_date, t.amount, t.[description], sum (t2.amount) as RunningTotal
FROM
cash_transaction_table t
CROSS JOIN cash_transaction_table t2
WHERE
(t2.transaction_date <= t.transaction_date)
GROUP BY
t.transaction_date,
t.amount,
t.[description]
ORDER BY
t.transaction_date