Lawfare Group → Knowledge Institute → Lawfare Academy

← Course syllabus · Related lesson

Keep the window, the row and the reporting period distinct

Current complete on-site edition 6

Required teaching and complete optional models are supplied on LawfareGroup.com. Publisher and primary links are optional provenance or further reading. Florida is the contemporary state-law default; federal rules and explicitly comparative accounting frameworks are distinguished. Independent subject review and observed human learner testing remain pending.

Cumulative data-system dossier, transfer models and 30-point rubric

Preserved original teaching integrated with complete current on-site preparation. Independent human subject review and observed learner testing remain pending.

Learning objectives

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.

Progressive optional practice

Attempt each problem before opening its model. Then revise the reasoning under the changed conditions.

Guided

List the monthly grouped totals and the transaction-by-transaction totals for B.

Compare the model reasoning

Monthly totals are January 200 and February 150. With the defined id order, transaction running totals are 200,300,350. The two output grains are different.

Changed conditions

Only posted_date is used with the default ordered frame.

Compare the revised analysis

The same-date February peers both include the full peer group and display 350.

Independent

Construct a February-only display whose cumulative measure includes January.

Compare the model reasoning

First compute the window over the needed history in a CTE or subquery; then apply the February display filter outside it. Expect A=400 and B=350 at the February endpoints.

Changed conditions

The measure is explicitly a February-only subtotal.

Compare the revised analysis

Filtering before aggregation is then consistent with the target; expect A=300 and B=150. Change the label, not just the SQL, when changing the estimand.

Transfer

An overnight import backdates a new transaction. What must a reproducible report preserve?

Compare the model reasoning

Define effective versus ingestion time, the as-of data snapshot and the ordering rule; recompute affected running values and reconcile to the ledger. A stable id alone does not establish business chronology.

Changed conditions

The report is intended to reproduce what staff knew last month.

Compare the revised analysis

Use the historical data vintage or an auditable knowledge-time model. Today’s corrected effective-date ledger may not reproduce the information available then.

Source readings and scope

SQLite: window functions — Reference for partitions, ordering, peers and frames. Sales ledger and boundary comparisons are original.

SQLite: SELECT processing — Reference for query processing and filtering.

SQLite: WITH clause — Reference for the CTE structure used to separate calculation from display.

Reproduce the worked examples in Python 3. Standard library only; synthetic data; no production records or network access.