A fictional banking app receives events for customer transactions. Each activity_id has a lifecycle: pending, then posted, and sometimes a reversal. Events may arrive out of order, and the amount or description can change between pending and posted. Build a canonical view that yields one row per activity with the latest state, and use it to compute accurate posted spend. The dataset is synthetic and illustrative.
SQL dataset setup
CREATE TABLE activity_events (event_id INTEGER NOT NULL, activity_id TEXT NOT NULL, event_type TEXT NOT NULL, event_ts TEXT NOT NULL, amount_cents INTEGER, description TEXT);
INSERT INTO activity_events (event_id, activity_id, event_type, event_ts, amount_cents, description) VALUES (101, 'A1', 'pending', '2026-11-01T10:00:00Z', 1200, 'Coffee Shop');
INSERT INTO activity_events (event_id, activity_id, event_type, event_ts, amount_cents, description) VALUES (102, 'A1', 'posted', '2026-11-02T08:00:00Z', 1250, 'Coffee Shop Inc');
INSERT INTO activity_events (event_id, activity_id, event_type, event_ts, amount_cents, description) VALUES (201, 'A2', 'pending', '2026-11-01T12:00:00Z', 5000, 'Online Retailer');
INSERT INTO activity_events (event_id, activity_id, event_type, event_ts, amount_cents, description) VALUES (202, 'A2', 'posted', '2026-11-02T09:00:00Z', 5000, 'Online Retailer');
INSERT INTO activity_events (event_id, activity_id, event_type, event_ts, amount_cents, description) VALUES (203, 'A2', 'reversal', '2026-11-03T09:00:00Z', 5000, 'Reversal: Online Retailer');
INSERT INTO activity_events (event_id, activity_id, event_type, event_ts, amount_cents, description) VALUES (301, 'A3', 'pending', '2026-11-04T15:00:00Z', 799, 'Subscription');
Latest event per activity
Write a read-only query that returns one row per activity_id with the latest event by event_ts. For ties, use the larger event_id. Include activity_id, event_type, amount_cents, description, and event_ts. Order by activity_id.
Show worked solution
WITH ranked AS (SELECT activity_id, event_type, amount_cents, description, event_ts, ROW_NUMBER() OVER (PARTITION BY activity_id ORDER BY event_ts DESC, event_id DESC) AS rn FROM activity_events) SELECT activity_id, event_type, amount_cents, description, event_ts FROM ranked WHERE rn = 1 ORDER BY activity_id
The canonical latest state for A1 is posted at 1250 cents with updated description; A2 is reversal; A3 remains pending. This yields one row per activity and is the basis for spend metrics. Pending and reversal activities are not counted as spend.
Posted spend excluding reversals
Using the latest event per activity, compute total posted spend in cents and the number of posted activities. Exclude activities whose latest state is reversal or pending. Return posted_spend_cents and posted_activity_count.
Show worked solution
WITH ranked AS (SELECT activity_id, event_type, amount_cents, ROW_NUMBER() OVER (PARTITION BY activity_id ORDER BY event_ts DESC, event_id DESC) AS rn FROM activity_events) SELECT COALESCE(SUM(CASE WHEN event_type = 'posted' THEN amount_cents END), 0) AS posted_spend_cents, COUNT(CASE WHEN event_type = 'posted' THEN 1 END) AS posted_activity_count FROM ranked WHERE rn = 1
Only A1 is posted with 1250 cents; A2 is reversed and A3 is pending, so spend is 1250 cents and count is 1. Denominator is posted activities. This avoids the overstatement caused by counting all events as purchases.
Design for replay and out-of-order arrival
Describe how you would make the canonical view replay-safe when the same events are reprocessed and when a reversal arrives before its posted event. Specify idempotency key, ordering rule, and how you would validate key uniqueness as an input assumption.
Show worked solution
Use event_id as the idempotency key and validate that it is unique across events as an input assumption. Order events by event_ts with event_id as deterministic tie-breaker, not arrival time. On replay, recompute canonical rows for affected activity_ids from the full event set for those activities, then overwrite the canonical table. For out-of-order reversal, the latest event_ts still determines final state; if reversal precedes posted in time, the latest state would be posted, so business rules must define precedence. Store a processed-event ledger keyed by event_id to avoid duplicates. Add a data-quality check that each activity's event sequence is non-decreasing in event_ts after sorting.