Skip to content

Instantly share code, notes, and snippets.

@rorymalcolm
Created May 1, 2023 14:26
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 rorymalcolm/4b2797a7ac81ab809eff56565515691c to your computer and use it in GitHub Desktop.
Save rorymalcolm/4b2797a7ac81ab809eff56565515691c to your computer and use it in GitHub Desktop.
with county_visits as (
select
timestamp,
county_name,
row_number() over (order by timestamp) as row_number
from joined
),
county_visits_with_next as (
select
county_visits.*,
lead(county_name) over (order by timestamp) as next_county_name,
lead(row_number) over (order by timestamp) as next_row_number
from county_visits
),
county_visits_with_next_and_end_time as (
select
county_visits_with_next.*,
lead(timestamp) over (order by timestamp) as end_time
from county_visits_with_next
),
county_visits_with_end_time as (
select
county_visits_with_next_and_end_time.*,
end_time - timestamp as duration
from county_visits_with_next_and_end_time
),
select * from county_visits_with_end_time where county_name != next_county_name;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment