I have a table like this below.
ID Time State
1 1 "active"
1 2 "active"
1 3 "active"
1 4 "inactive"
2 2 "inactive"
2 3 "active"
3 1 "active"
3 3 "active"
3 4 "inactive"
I want to sort table with start/end time by state.
It might need lag()
window function but I don't know how to find end point of same state.
My expected table should look like this.
ID Start End State
1 1 4 "active"
1 4 NULL "inactive"
2 2 3 "inactive"
2 3 NULL "active"
3 1 4 "active"
3 4 NULL "inactive"