merge DATE-rows if episodes are in direct succession or overlapping

后端 未结 4 897
我寻月下人不归
我寻月下人不归 2021-01-14 07:47

I have a table like this:

ID    BEGIN    END

If there are overlapping episodes for the same ID (like 2000-01-01 - 2001-1

4条回答
  •  半阙折子戏
    2021-01-14 08:33

    I'm not making full sense of your question, but I'm absolutely certain that you need to look into the lead()/lag() window functions.

    Something like this, for instance, will be a good starting point to place in a subquery or a common table expression, in order to identify whether rows overlap or not per id:

    select id,
           lag(start) over w as prev_start,
           lag(end) over w as prev_end,
           start,
           end,
           lead(start) over w as next_start,
           lead(end) over w as next_end
    from yourtable
    window w as (
           partition by id
           order by start, end
           )
    

提交回复
热议问题