Skip to content

Instantly share code, notes, and snippets.

@MikeMKH
Created May 30, 2018 19:14
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 MikeMKH/05a228127672db3be113d3292d935db4 to your computer and use it in GitHub Desktop.
Save MikeMKH/05a228127672db3be113d3292d935db4 to your computer and use it in GitHub Desktop.
SQL example of using lag to find the date that a column changes in a time series data
select * from (
select
key_col
,date_col
,data_col
,prev_data_col = lag(data_col, 1) over (partition by key_col order by date_col)
from tbl
) as z
where prev_data_col is not NULL
and data_col <> prev_data_col
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment