The situation
A source system sends a row to customer_updates every time a customer record changes.
The downstream customers table must hold the current state: one row per customer.
The feed is not clean. The same row is sometimes delivered twice, some corrections
arrive with the same updated_at as the row they correct, and some rows have no
updated_at at all.
Input tables
customer_updates
One row per delivered change. A customer has one or more rows.
| Column | Type | Description |
|---|---|---|
customer_id |
BIGINT | The customer the change is for. |
email |
STRING | Email after the change. |
city |
STRING | City after the change. NULL means the customer has no city on record. |
updated_at |
TIMESTAMP_NTZ | When the change happened in the source system. Can be NULL. |
ingested_at |
TIMESTAMP_NTZ | When the row arrived in the feed. Never NULL. |
Requirement
Return the current row for every customer.
- The current row is the one with the latest
updated_at. - If two rows of a customer have the same
updated_at, the one with the lateringested_atwins. It is a correction of the other. - A row with no
updated_atis older than any row that has one. It is only the current row if the customer has no dated rows. - A row delivered twice must not produce two output rows.
- Take all output columns from the same input row. If the current row has a NULL
city, the output has a NULLcity.
Expected output
Exactly one row per customer.
| Column | Type | Description |
|---|---|---|
customer_id |
BIGINT | |
email |
STRING | From the current row. |
city |
STRING | From the current row. Can be NULL. |
updated_at |
TIMESTAMP_NTZ | From the current row. Can be NULL. |
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. 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
"One row per key, picked by a rule" is a job for ROW_NUMBER(). Number the rows of
each customer so that the wanted row gets number 1, then keep number 1.
SELECT customer_id, email, city, updated_at
FROM (
SELECT
*,
ROW_NUMBER() OVER (
PARTITION BY customer_id
ORDER BY updated_at DESC NULLS LAST, ingested_at DESC
) AS rn
FROM customer_updates
) ranked
WHERE rn = 1
The work is all in the ORDER BY. It has to turn every rule of the requirement into a
sort key, in priority order:
updated_at DESC: the latest change first.NULLS LAST: a row withoutupdated_atloses to any dated row.ingested_at DESC: among rows with the sameupdated_at, the correction first.
ROW_NUMBER() hands out 1 exactly once per customer, so the output cannot have two
rows for a customer, whatever the data looks like. A row delivered twice needs no
special handling: both copies are identical, one gets number 1, the other number 2.
In Spark, DESC already sorts NULLs last, so leaving out NULLS LAST gives the same
result here. Write it anyway. It tells the next reader the NULL case was thought about,
and the default differs between engines.
Common wrong answers
The feed has 120 rows for 40 customers, so the correct output has 40 rows.
MAX(updated_at) per customer, joined back to the feed. Returns 47 rows. The join
matches every row that has the maximum timestamp: both rows of a tie (5 customers) and
both copies of a latest row delivered twice (4 customers). It also loses customers 239
and 240, whose only row has a NULL updated_at: their maximum is NULL, and
updated_at = NULL never matches. Adding DISTINCT removes the repeated copies but
not the ties or the missing customers, and gives 43 rows.
RANK() instead of ROW_NUMBER(). Returns 49 rows. RANK() gives the same number
to rows that tie on the sort keys, so every tie and every repeated latest row comes
through twice.
ROW_NUMBER() ordered by updated_at DESC only. Returns 40 rows, but which of two
tied rows gets number 1 is left to chance. On this data it picks the wrong city for
at least one of the five tied customers. This is the dangerous version: the row count is right,
it can pass a test today and return a different row after the data is repartitioned.
If the sort keys do not identify a single row, the query is not deterministic.
Ordering by ingested_at DESC only. Arrival order is not change order. Customers
207, 220 and 233 have a row without updated_at that arrived last; this version makes
it the current row.
MAX() on every column with GROUP BY customer_id. Returns 40 rows, each of them
possibly a row that never existed: the largest email from one version next to the
largest city from another. It also cannot return a NULL city when an older version
had one (customers 210 and 225). Pick a row first, then read its columns.
SELECT DISTINCT. Removes only the 8 identical copies and returns 112 rows. It
does nothing about older versions.
Edge cases in the data
| Case | Customers | What it catches |
|---|---|---|
Two rows with the same updated_at, different city |
203, 211, 219, 226, 234 | No tie-breaker; RANK(); MAX join. |
| Latest row delivered twice | 205, 214, 222, 231 | MAX join and RANK() return both copies. |
| An older row delivered twice | 208, 217, 229, 238 | SELECT DISTINCT treated as the whole solution. |
Extra row with NULL updated_at that arrived last |
207, 220, 233 | Ordering by ingested_at; NULLS FIRST. |
Only row has NULL updated_at |
239, 240 | MAX join drops the customer. |
Latest row has NULL city |
210, 225 | MAX(city), or "fixing" NULLs with an older value. |