Measure how many days each model version stayed live with LEAD

MediumSQL

Why it matters Version lifetimes, time between deploys and time to next event are standard MLOps and product metrics.

A model registry logs each release. How long each version stayed in production is the gap until the next release of the same model, which LEAD reads directly.

Write query() returning a SQLite SQL string. Table releases(model TEXT, version TEXT, released_on TEXT) with released_on as 'YYYY-MM-DD', one row per release, no two releases of a model on the same day.

Return (model, version, released_on, days_live) for every release, where days_live is the whole number of days until the next release of the SAME model, and NULL for the model's latest release. Order by model ascending, then released_on ascending.

Signaturedef query()

Example

model 'chat' v1 on 2026-01-01, v2 on 2026-01-11 gives [('chat', 'v1', '2026-01-01', 10), ('chat', 'v2', '2026-01-11', None)].

Understand the idea

LAG and LEAD read a value from a neighbouring row in the window: LAG looks back, LEAD looks forward. With PARTITION BY, the neighbour is always from the same group, and at the edge of a group the result is NULL.

Worked example: chat releases on Jan 1 (v1) and Jan 11 (v2). For v1, LEAD(released_on) is Jan 11, and julianday('2026-01-11') - julianday('2026-01-01') = 10. For v2 there is no next row, so LEAD is NULL and days_live is NULL.

Rule: next_value = LEAD(col, 1) OVER (PARTITION BY key ORDER BY time). Gap in days = julianday(next) - julianday(this). SQLite stores dates as text, so use julianday for date math.

MLOps dashboards use this for time between deploys, and product teams use it for time to the user's next visit.

solution.pyPython idle
Ctrl/Cmd + Enter runs sample tests

Run checks the first 2 tests. Submit checks all 3.

  • Sample 1: example_run1(query())
  • Sample 2: several models interleaved_run2(query())
  • Test 3: across a year end_run3(query())