Deduplicate a raw events table
Keep exactly one row per event_id — the latest by timestamp — from a bronze table that receives replays and retries.
Scenario
NorthBridge Retail's ingestion retries on timeout, so the bronze events_raw table contains duplicates: same event_id, sometimes with a newer received_at from the retry. Build the query that returns one clean row per event.
Input data
Table: events_raw
| event_id | event_type | amount | received_at |
|---|---|---|---|
| E-1 | purchase | 120.00 | 2026-10-01 09:00:00 |
| E-1 | purchase | 120.00 | 2026-10-01 09:02:10 |
| E-2 | refund | -40.00 | 2026-10-01 09:05:00 |
| E-3 | purchase | 75.50 | 2026-10-01 09:06:30 |
| E-3 | purchase | 75.50 | 2026-10-01 09:06:30 |
| E-4 | purchase | 210.00 | 2026-10-01 09:08:00 |
Setup script
CREATE OR REPLACE TABLE events_raw (
event_id STRING,
event_type STRING,
amount DECIMAL(10,2),
received_at TIMESTAMP
);
INSERT INTO events_raw VALUES
('E-1', 'purchase', 120.00, '2026-10-01 09:00:00'),
('E-1', 'purchase', 120.00, '2026-10-01 09:02:10'),
('E-2', 'refund', -40.00, '2026-10-01 09:05:00'),
('E-3', 'purchase', 75.50, '2026-10-01 09:06:30'),
('E-3', 'purchase', 75.50, '2026-10-01 09:06:30'),
('E-4', 'purchase', 210.00, '2026-10-01 09:08:00');Task
- Return one row per
event_id. - When duplicates differ, keep the one with the latest
received_at. - The result must be deterministic even for exact-duplicate rows (E-3).
Expected output
| event_id | event_type | amount | received_at |
|---|---|---|---|
| E-1 | purchase | 120.00 | 2026-10-01 09:02:10 |
| E-2 | refund | -40.00 | 2026-10-01 09:05:00 |
| E-3 | purchase | 75.50 | 2026-10-01 09:06:30 |
| E-4 | purchase | 210.00 | 2026-10-01 09:08:00 |
Constraints
- One statement; no intermediate tables.
GROUP BYon all columns is not acceptable (it breaks the moment duplicates differ in any column).
Hints
Hint 1
You need to rank rows within each event_id group and keep the best one. Which family of functions ranks rows within groups?
Hint 2
ROW_NUMBER() OVER (PARTITION BY event_id ORDER BY received_at DESC) gives the latest row rank 1. Why is ROW_NUMBER safer than RANK here?
Solution
Show solution
-- tested on: Databricks Runtime 15.x LTS
WITH ranked AS (
SELECT *,
ROW_NUMBER() OVER (
PARTITION BY event_id
ORDER BY received_at DESC
) AS rn
FROM events_raw
)
SELECT event_id, event_type, amount, received_at
FROM ranked
WHERE rn = 1
ORDER BY event_id;Explanation
ROW_NUMBER assigns exactly one rank-1 row per partition even on ties (E-3's two identical rows), so the result is always one row per key. RANK would return both tied rows and your dedupe silently stops deduping.
Alternative approaches
QUALIFY rn = 1(Databricks SQL) drops the CTE.- For streaming ingestion,
dropDuplicates(["event_id"])with watermarking solves it before bronze is written.
Performance notes
At billions of rows this shuffles on event_id. If dedupe runs per ingestion batch, dedupe the small batch before merging into the big table instead of deduping the big table.
Follow-up questions
- How does the answer change if a late retry can arrive in the next batch?
- What tie-breaker would you add if
received_atcan be identical for genuinely different payloads?
Learn the concept
Keep going
Window functions you will actually use in pipelines
Four window patterns cover almost every pipeline need: latest-row dedupe, sessionization, running totals and gap detection — with the performance traps of each.
Build an SCD Type 2 with MERGE
Apply a day's customer address changes to a dimension table, closing old versions and inserting new ones, in a single MERGE.
Deduplicate Late-Arriving CDC Rows with MERGE: A Worked Problem
A CDC feed sends duplicates and out-of-order changes. Build a MERGE that keeps the latest row per key, ignores late events and is safe to replay.