Created
May 30, 2018 19:14
-
-
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
This file contains hidden or 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
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