Skip to main content
SQL โ€ข LESSON 183

Latest Customer State

How can we reconstruct the latest customer state from an event log stream?

Advancedโฑ 3 Minutesโญ 1960 XP
๐Ÿค” THE QUESTION

How can we reconstruct the latest customer state from an event log stream?

๐Ÿ’ก WHAT IS IT?

Partitioning customer events by customer_id extracts the latest snapshot state for each customer.

๐ŸŽฏ WHAT IS IT USED FOR?

Event sourcing snapshotting, Change Data Capture (CDC) state reconstruction, customer 360 views.

๐Ÿ’ป EXAMPLE
SELECT *
FROM (
    SELECT
        *,
        ROW_NUMBER() OVER (
            PARTITION BY customer_id
            ORDER BY updated_at DESC
        ) AS rn
    FROM customer_events
) latest
WHERE rn = 1;

๐ŸŽฏ Mission Objectives

Practice typing production-grade SQL code for Latest Customer State.

  • โœ“ Event sourcing snapshot
  • โœ“ Customer 360 state
  • โœ“ CDC reconstruction
  • โœ“ Window ranking