Measure how many days each model version stayed live with LEAD
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.
def 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.
Similar problems
- Count failed training runs per project, keeping projects with zero failuresMedium
- Pick the best scoring model for each eval task with ROW_NUMBERMedium
- Pivot request statuses into columns with conditional aggregationMedium
- Find eval tasks where a candidate model scores below the baseline with a self joinMedium