/swarm-grouped-countries.sql Secret
Created
June 8, 2023 18:52
Star
You must be signed in to star a gist
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Useful sql. Is it possible for you to provide a sample dataset of the tables checkins and venues