The situation
The CRM writes a row to customer_saves every time someone presses Save on a customer
record, whether or not anything changed. Analysts need the history of where each
customer lived as a type 2 slowly changing dimension: one row per period during which
the city stayed the same.
Input tables
customer_saves
One row per save. A customer has at most one save per date.
| Column | Type | Description |
|---|---|---|
customer_id |
BIGINT | The customer. |
city |
STRING | The city on the record after the save. NULL means no city on record. |
saved_on |
DATE | The date of the save. |
Requirement
Return one row per version of a customer's city.
- A new version starts at the customer's first save, and at every save where the city is different from the city of the previous save.
- A save that leaves the city unchanged does not start a new version.
- NULL is a value like any other here. Going from a city to NULL, or from NULL to a city, is a change. Two consecutive saves with a NULL city are not.
- If a customer returns to a city they lived in before, that is a new version. The two stays are separate rows.
valid_fromis thesaved_onof the save that started the version.valid_tois thevalid_fromof the customer's next version. The latest version hasvalid_to= NULL.is_currentis true for the latest version of each customer and false otherwise.
Expected output
One row per customer version.
| Column | Type | Description |
|---|---|---|
customer_id |
BIGINT | |
city |
STRING | Can be NULL. |
valid_from |
DATE | |
valid_to |
DATE | NULL for the current version. |
is_current |
BOOLEAN |
Solve it in the browser
This editor runs DuckDB inside your browser. Nothing is installed and nothing is sent
to a server. DuckDB's SQL is close to Spark SQL but not identical. Spark's null-safe comparison <=> does not exist in DuckDB. Write IS NOT DISTINCT FROM (or IS DISTINCT FROM) here.
The Colab notebook runs real Spark.
Your result appears here. Your draft is kept in this browser.
Explanation
Show explanationGives the answer away. Try the problem first.
The approach
The saves are a stream of "the city is now X" statements. Most of an SCD2 build is deciding which of those statements start a new version.
Step 1: find the saves that start a version. Compare each save's city with the city of the customer's previous save.
WHERE save_number = 1 OR NOT (city <=> prev_city)
prev_city comes from LAG(city). Two details matter:
<=>is the null-safe equality operator.NULL <=> NULLis true and'Pune' <=> NULLis false. With the ordinary<>, any comparison involving NULL is NULL, and aWHEREclause treats NULL as "no".- The first save needs its own condition (
ROW_NUMBER() = 1). You cannot rely onprev_citybeing NULL to detect it, because a first save with a NULL city would compare as equal to its missing predecessor.
Step 2: after dropping the saves that changed nothing, each remaining row is the start of one version. A version ends where the next one starts:
LEAD(valid_from) OVER (PARTITION BY customer_id ORDER BY valid_from) AS valid_to
LEAD returns NULL for the last version, which is exactly the "still current" marker,
so is_current is valid_to IS NULL.
The order of the two steps matters. LEAD has to run after the no-op saves are gone,
otherwise it finds the next save instead of the next version.
The 123 saves of 40 customers produce 85 versions.
Common wrong answers
city <> prev_city. Returns 80 rows. Every change to or from NULL is missed,
because the comparison is NULL rather than true. Customer 318 goes Mumbai, NULL, NULL,
Pune and should have three versions; this gives one.
GROUP BY customer_id, city with MIN(saved_on). Returns 77 rows. It merges two
separate stays in the same city into one. Customer 303 lived in Pune, moved to Mumbai
and came back: three versions, not two. The second Pune stay also ends up with the
wrong dates.
LEAD directly on the saves, without removing no-ops. Returns 123 rows, one per
save. A customer who pressed Save three times in Bengaluru gets three "versions".
WHERE NOT (city <=> prev_city) without the first-row condition. Returns 83 rows.
Customers 306 and 335 start with a NULL city; their first save looks unchanged and
their first version is lost. For 335, that is the whole customer.
valid_to set to the day before the next valid_from. Row count is right, values
in valid_to are wrong. Both conventions exist in real warehouses. This problem asks
for the exclusive end, where one version's valid_to equals the next one's
valid_from; it makes "which version was valid on date D" a simple
valid_from <= D AND (D < valid_to OR valid_to IS NULL). Always check which one the
target table uses.
Edge cases in the data
| Case | Customers | What it catches |
|---|---|---|
| Moves away and back to the same city | 303, 312, 327 | GROUP BY city. |
| City removed (NULL), then set again | 318, 331 | <> instead of <=>. |
| Two NULL saves in a row | 318 | Treating every NULL as a change. |
| First save has a NULL city | 306, 335 | Detecting the first row through prev_city IS NULL. |
| Saved several times, never changed | 309 | No collapsing of no-op saves. |
| Single save | 335, 340 | LEAD returning NULL must mean current, not missing. |