runtime
47 / 88

task History, SCD & as-of · hard

Build a region history from snapshots

shopper_snapshots records each shopper's region at month end. Return shopper_id, region, valid_from and valid_to — one row per unbroken run of the same region.

what to return

Columns
shopper_idregionvalid_fromvalid_to in this order
Rows
11 rows · any order
Watch for
valid_from is the run's first snapshot_date; valid_to is the first snapshot_date of the shopper's next run, or NULL for the latest run.
A missing region (NULL) is its own value: consecutive NULL snapshots form one run.
If a shopper returns to an earlier region later, that's a new run.
Skill
Compress snapshots into change history. Refresher: LEAD & the next event →

expected result

Your query should return exactly these columns and values. Run it and compare.

expected result11 rows · any order
shopper_idregionvalid_fromvalid_to
1West2025-01-312025-04-30
1East2025-04-30NULL
2East2025-01-31NULL

first 3 of 11 rows

data you can query

Sample rows from each table. Each column shows its type underneath; → marks a link to another table.

shopper_snapshots3 columns · 20 rows
snapshot_datetext · keyshopper_idinteger · keyregiontext
2025-01-311West
2025-02-281West
2025-03-311West
2025-04-301East
2025-01-312East

showing 5 of 20 rows

solution.sql
 

Hints and the answer are always available. Using them counts the question as solved with help.