Sign in to run and submit your work
Reading is open to everyone. Running code and saving drafts need an account so your work is yours and comes back on your next visit.
or
CODE WORKSPACE
Return exactly customer_id, old_tier, new_tier, old_valid_from, old_valid_to, new_valid_from, and new_valid_to for current history rows whose staged attributes changed.
Result columns · in this order
customer_idold_tiernew_tierold_valid_fromold_valid_tonew_valid_fromnew_valid_toHow to approach it
Join clean_stage to dim_customer_history where is_current = 1, then compute the close and open boundaries.
Sample input
| customer_id | tier | valid_from | valid_to | is_current |
|---|---|---|---|---|
| 101 | silver | 2025-01-01 | 9999-12-31 | 1 |
| 104 | gold | 2026-02-20 | 9999-12-31 | 1 |
| 105 | bronze | 2025-12-01 | 9999-12-31 | 1 |
3 rows — all rows shown.
| customer_id | tier | updated_at |
|---|---|---|
| 101 | gold | 2026-03-05 10:00:00 |
| 104 | platinum | 2026-03-05 12:30:00 |
| 105 | bronze | 2026-03-05 08:00:00 |
3 rows — all rows shown.
Expected output
| customer_id | old_tier | new_tier | old_valid_from | old_valid_to | new_valid_from | new_valid_to |
|---|---|---|---|---|---|---|
| 101 | silver | gold | 2025-01-01 | 2026-03-04 | 2026-03-05 | 9999-12-31 |
| 104 | gold | platinum | 2026-02-20 | 2026-03-04 | 2026-03-05 | 9999-12-31 |
2 rows — all rows shown.
Constraints
Use the latest staged non-deleted row per customer. Join only dim_customer_history rows where is_current = 1. Close the old row one day before the staged updated_at date, open the new row on updated_at's date, and order by customer_id.
Expected skills
SCD2 change detection, validity ranges, and current-row history reasoning.
Submit for review to find out what your query gets right, what it gets wrong, and how it compares with the best working query for this exercise.
This scenario runs a full workspace — editor, canvas and results side by side. It needs a laptop or desktop to be usable. Open this page on a bigger screen to start building.