Skip to content
Advertisement

How can i get a gap from a history table for repeating groups over time

I have a historical table with data like as bellow :

The EXP_DT is null mean that the row is active.

I want to return a group of data each time Status changes

The expected result like as bellow :

Thanks for help !

Advertisement

Answer

This is classic gaps-and-islands problem.

We can solve it by using LAG to check for differences, then a windowed COUNT to get the GAP number.

You may want to add a partitioning clause, such as PARTITION BY ID

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement