The situation
The statements team needs the account balance after every transaction. An AI assistant wrote the query in the answer cell. It runs, returns one row per transaction, and for many accounts every number is right.
Support has a complaint from a customer: two transactions on the same day both show the same balance, and it is not the balance after either of them alone.
Find the bug and fix it.
Input tables
transactions
One row per transaction.
| Column | Type | Description |
|---|---|---|
txn_id |
BIGINT | Unique. Within a date, a lower txn_id was processed earlier. |
account_id |
BIGINT | The account. |
txn_date |
DATE | The date of the transaction. An account can have several on one date. |
amount |
DECIMAL(10,2) | Positive for money in, negative for money out. |
Requirement
For every transaction, return the account's balance immediately after it.
- Every account starts at 0.
- Transactions of an account are applied in order of
txn_date, and within a date in order oftxn_id. balance_afteris the sum of the account's amounts up to and including that transaction.
Expected output
One row per transaction.
| Column | Type | Description |
|---|---|---|
txn_id |
BIGINT | |
account_id |
BIGINT | |
txn_date |
DATE | |
amount |
DECIMAL(10,2) | |
balance_after |
DECIMAL(12,2) | Balance right after this transaction. |
The AI-written code
-- Running balance per account: cumulative sum of amounts in date order SELECT txn_id, account_id, txn_date, amount, CAST( SUM(amount) OVER ( PARTITION BY account_id ORDER BY txn_date ) AS DECIMAL(12, 2) ) AS balance_after FROM transactions
Explanation
Show explanationGives the answer away. Try the problem first.
The bug
SUM(amount) OVER (PARTITION BY account_id ORDER BY txn_date)
A window with ORDER BY and no explicit frame gets this default frame:
RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
RANGE works on values, not on rows. "Current row" in a RANGE frame means the
current row and every other row with the same ORDER BY value. The query orders by
txn_date only, so all transactions of an account on the same date are peers. Each of
them gets the sum up to the end of that date.
That is exactly the complaint: two transactions on one day show the same balance, and it is the balance after both.
Why it is easy to miss:
- An account whose transactions are all on different dates has no peers, so every number is right. That is 4 of the 25 accounts here.
- For the other 21 accounts, only the rows that share a date with a later transaction are off: 46 of the 203 rows. The last transaction of each date is always right.
- Daily closing balances are correct, so any reconciliation at day level passes.
The fix
Say which rows you mean, and make the order unique:
SUM(amount) OVER (
PARTITION BY account_id
ORDER BY txn_date, txn_id
ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
)
Either change alone needs care:
- Adding
txn_idto theORDER BYand keeping the default frame also passes. With a unique ordering no row has peers, soRANGEandROWSmean the same thing. - Adding
ROWS ...and still ordering bytxn_datealone is not a fix. The frame is now row-based, but Spark is free to put two same-date transactions in either order. The result can change between runs, and on this data it comes out wrong.
Do both. A running total needs a total order.
The same default frame is behind the well-known LAST_VALUE surprise: with only an
ORDER BY, LAST_VALUE returns the current row's own value (or a peer's), because
the frame stops there.
Common wrong answers
ROWS frame with ORDER BY txn_date. Described above: not deterministic, and
wrong on this data.
Ordering by txn_id alone. This passes here, because the ids in this table happen
to increase with the date. The statement only promises the order of ids within a date.
A late-posted transaction with a high id and an old date would break it. Order by the
business rule you were given: date first, then id.
Dropping PARTITION BY account_id. One running total across all accounts.
Edge cases in the data
| Case | Where | What it catches |
|---|---|---|
| Several transactions on one date | 21 of 25 accounts | The default RANGE frame. |
| No two transactions on the same date | accounts 704, 711, 718, 723 | Shows why spot checks pass. |
| Money in and out on the same date | many accounts | Makes the peers' shared balance visibly wrong for both rows. |
txn_id order within a date differs from row order in the file |
everywhere | Relying on input order instead of ORDER BY. |