Skip to content
Advertisement

extract “spells” from observed dates in PostgreSQL

I have a table like this

I’d like to infer non-overlapping spells, i.e. a table that looks like.

In python, I would do something like

But am not sure how to achieve the same result in SQL.

Advertisement

Answer

This is a gaps and islands problem. The simplest way is to use row_number():

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