129 words
1 minute
Lag window function in SQLite
Anubhav Gain
2024-05-22

Lag window function in SQLite#

Here’s how to use a lag window function to calculate new cases per day when the table just has total cases over time on different dates.

The key clause is this:

select
day,
confirmed - lag(confirmed, 1) OVER (
ORDER BY
day
) as new_cases

So the syntax is lag(column, 1) over (order by day) - to get the previous value of column based on the day.

Full example query (using a CTE as well):

with italy as (
select
rowid,
day,
country_or_region,
province_or_state,
admin2,
fips,
confirmed,
deaths,
recovered,
active,
latitude,
longitude,
last_update,
combined_key
from
johns_hopkins_csse_daily_reports
where
"country_or_region" = :p0
order by
confirmed desc
)
select
day,
confirmed - lag(confirmed, 1) OVER (
ORDER BY
day
) as new_cases
from
italy
order by day desc limit 50

Originally tweeted here: https://twitter.com/simonw/status/1246482954630492200

Lag window function in SQLite
https://mranv.pages.dev/posts/lag-window-function-in-sqlite/
Author
Anubhav Gain
Published at
2024-05-22
License
CC BY-NC-SA 4.0