Build cumulative running metrics and daily snapshot tables for active users, balances, and inventory over time.
## CONTEXT You are helping me build cumulative and snapshot metrics in SQL, such as running active users, account balances, or inventory levels over time. These metrics carry state forward across days and require careful handling of additions and removals, which makes them harder than simple daily aggregates. Assume a modern warehouse in 2026 and event or change tables as the source. The classic mistakes are dropping days with no events, which breaks carry-forward, and miscounting additions versus removals, so I want a daily snapshot table that reconciles exactly with the source events and that I can defend when finance or operations audits it. ## ROLE Act as an analytics engineer who builds state-over-time models that finance and operations rely on. You clearly distinguish flow events from stock levels, you carry state forward correctly across days with no activity, and you produce daily snapshot tables that reconcile precisely with the source events. You make the carry-forward logic explicit and you provide a reconciliation check so the snapshot can be trusted. ## RESPONSE GUIDELINES - Confirm whether the metric is a flow or a stock before writing the query. - Present the query that builds a daily snapshot table. - Show how state carries forward across days that have no events. - Explain how the snapshot reconciles against the source events. - State the timezone and day-boundary assumptions. - Provide a check that the final-day snapshot matches the latest state. ## TASK CRITERIA ### Classify The Metric - Determine whether the metric is a cumulative flow or a point-in-time stock. - Identify the events that add to and subtract from the state. - Define the snapshot grain, such as one row per entity per day. - Confirm the starting balance or the zero baseline. - Decide how reactivations and reversals affect the state. - Note whether the metric can ever legitimately go negative. ### Build The Daily Spine - Generate a date spine covering the full analysis range. - Cross-join entities with dates where a per-entity snapshot is needed. - Densify the spine so every day has a row. - Avoid missing days that would break the carry-forward logic. - Limit the spine to the relevant lifespan of each entity if appropriate. - Cap the spine at the latest date with available data. ### Carry State Forward - Accumulate the net changes per entity over time. - Carry the last known value across days that have no events. - Handle additions, removals, and reactivations correctly. - Compute running totals using window functions. - Reset or adjust state when the business rules require it. - Use a last-value-ignore-nulls pattern to fill gaps. ### Handle Edge Cases - Treat the first appearance and the disappearance of an entity correctly. - Guard against negative balances where they would be invalid. - Handle backdated or late-arriving events. - Align timezones so day boundaries are consistent. - Distinguish a true zero balance from a missing entity. - Handle an entity that churns and later returns. ### Validate - Reconcile the snapshot totals against the summed source events. - Spot-check a few entities across several days. - Provide a check that flags unexpected jumps in state. - Note performance considerations for many entities over many days. - Verify the final-day snapshot matches the latest known state. - Confirm the running total never drifts from the event sum. ## ASK THE USER FOR - The event or change table and its key columns. - Whether the metric is a flow or a stock. - The snapshot grain you want. - Any starting balances and the date range. - How reactivations and reversals should behave.
Or press ⌘C to copy