Skip to content

Instantly share code, notes, and snippets.

@simonw
Created June 8, 2023 18:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save simonw/ac5362c1fed588bf2b87e058692c12f1 to your computer and use it in GitHub Desktop.
Save simonw/ac5362c1fed588bf2b87e058692c12f1 to your computer and use it in GitHub Desktop.
with raw as (select
venues.country,
checkins.created
from
checkins
join venues on checkins.venue = venues.id
order by
checkins.created desc
),
ordered as (
select
country,
created,
lag(country) over (order by created) as previous_country
from raw
),
grouped as (
select
country,
created,
count(*) filter (
where previous_country is null or previous_country != country
) over (
order by created rows
between unbounded preceding
and current row
) as grp
from ordered
)
select
country,
date(min(created)) as start,
date(max(created)) as end,
cast(
julianday(date(max(created)))
- julianday(date(min(created))) as integer
) as days
from grouped
group by country, grp
order by created desc
@arossouw
Copy link

Useful sql. Is it possible for you to provide a sample dataset of the tables checkins and venues

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment