Keep the first-ingested copy of each event

mediumPipelines~20 min · 5 tests · ridelog

The ridelog app sends events to a collector that retries on timeout. About 5% of events landed twice. A copy has the same user_id, event_type and event_ts as the original, but its own event_id and a later ingested_at.

Downstream jobs need each event exactly once. Return the rows of events with duplicates removed:

column type meaning
event_id integer id of the copy you kept
user_id integer as in events
event_type text as in events
event_ts timestamp as in events
ingested_at timestamp as in events

Rules:

  • Two rows are copies of one event when user_id, event_type and event_ts all match.
  • Keep the copy with the earliest ingested_at.
  • If two copies were ingested at the same instant, keep the one with the lower event_id.
  • Don’t assume the first copy has the lower event_id. Ids come from the collector, not the clock.
  • Two different event types at the same timestamp are two events. Keep both.

Row order doesn’t matter.

Tables: events.

Learn the ideaRead the lesson →
solution.sqlDuckDB · ridelog

Press ⌘ ↵ or Run to check the visible tests. Submit runs the hidden ones too. Everything runs in your browser.

⌘ ↵ to run