An aggregate collapses rows; a window can retain them
A sales ledger contains A: January 100, February 300; B: January 200, February 100 and February 50. Each transaction has a unique id, and B’s two February transactions share the same posting date. GROUP BY representative,month produces four monthly rows: A January 100, A February 300, B January 200 and B February 150. The grouping grain differs from the five-row transaction grain.
A window SUM can attach a representative’s cumulative value to each transaction without collapsing those rows. That distinction matters when the dashboard must display the individual transaction and a running balance beside it. Write the intended output grain before selecting the calculation. If multiple transactions per month are meaningful, a monthly GROUP BY destroys their separate identities. If only monthly summaries are needed, computing a window on raw transactions and then removing arbitrary duplicates is a poor substitute for a clear monthly aggregation.
For a deterministic transaction running total, use SUM(amount) OVER(PARTITION BY rep ORDER BY posted_date,id ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW). The partition identifies whose history is accumulated, the ordering defines the transaction sequence, and the frame states which ordered rows contribute at each point. These are separate choices. A global running total would omit the rep partition and would answer a different question.
Peers can share a result under the default frame
In SQLite, an ordered aggregate window’s default frame includes the current row’s peers. If the ordering expression is only the posting date, B’s two same-date February transactions are peers. Their default-frame cumulative totals both include both transactions, so both display 350. That can be correct for a date-end balance, but it is not a transaction-by-transaction sequence of 300 followed by 350.
The explicit ROWS frame with ORDER BY posted_date,id makes the intended sequence unambiguous in this fixture: B January 200, first February transaction 300, second February transaction 350. Merely selecting ROWS while leaving tied rows unordered does not define which peer goes first. Use a stable tie-breaker that actually represents the intended sequence, and document what happens when transactions are corrected or backdated. An ingestion identifier may be deterministic while still differing from the business-effective order.
When the desired grain is monthly, aggregate first in a common table expression, then apply a window ordered by month to that monthly relation. A gets cumulative monthly totals 100 and 400; B gets 200 and 350. Do not join a monthly running total back onto raw lines and sum it again: the attached total would be repeated across transactions and counted multiple times. A value that is correct at one grain can become incorrect when aggregated at another.
The reporting boundary can truncate the history
Suppose the report displays only February but the intended running total includes January. A WHERE condition selecting February before the window removes January from the window’s input. The resulting totals are A=300 and B=150 for February, not the intended cumulative values 400 and 350. The window cannot see rows already excluded from its query input.
Compute the cumulative values in a CTE or subquery over the necessary history, then apply the February display filter outside that calculation. This yields the correct full-history February endpoints while showing only the requested rows. The correct boundary depends on the definition: a February-only subtotal intentionally excludes January and is not an error. Name the measure so users do not confuse a period subtotal with an as-of cumulative balance.
History also needs an explicit beginning. A query over only the current fiscal year cannot produce a lifetime cumulative balance without an opening balance or earlier transactions. Likewise, filtering out reversed or canceled transactions may be correct only if the corresponding reversal semantics are modeled consistently. Missing transaction amounts are not automatically zero, and SUM’s handling of NULL does not certify ledger completeness. Carry relevant missingness and reconciliation checks alongside the displayed measure.
A robust review includes tied dates, more than one transaction in a period, more than one representative, an empty period and a transaction arriving late. Reproduce expected results on the small fixture before interpreting a complex production plan. Then assess performance and indexing separately using representative workloads. The runnable case proves the stated SQLite results for these synthetic rows; it makes no timing, production scalability or concurrency claim.
Worked example: Two valid February displays with different definitions
Computing the window on all monthly history and then selecting February returns A=400 and B=350. Selecting February before the window returns A=300 and B=150. Both calculations are internally consistent. The former is a cumulative endpoint; the latter is a period-only total. The dashboard label and query boundary must agree.
Worked example: Same-date peers versus ordered transactions
For B, a date-ordered default frame gives 350 on both February rows. An explicit ROWS frame ordered by date and unique transaction id gives 300 and then 350. If the same two transactions are displayed in the opposite business order, the intermediate total changes even though the final total remains 350. Endpoint reconciliation alone cannot validate the claimed intermediate sequence.