Skip to content
Advertisement

SQL query to find gaps within a column of dates

I have a table with status and date for every day and I’m trying to find out when the statuses change and if there’s gaps within each status change / how many days were of a certain status.

Expected output:

Advertisement

Answer

This is a type of gaps-and-islands problem. In this case, subtracting a sequential number from each day is probably the simplest solution for identifying the “islands”:

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