How can I identify groups of consecutive dates in SQL?

后端 未结 2 2065
夕颜
夕颜 2021-02-14 04:54

Im trying to write a function which identifies groups of dates, and measures the size of the group.

I\'ve been doing this procedurally in Python until now but I\'d like

2条回答
  •  遇见更好的自我
    2021-02-14 05:26

    You can do this with a clever application of window functions. Consider the following:

    select name, date, row_number() over (partition by name order by date)
    from t
    

    This adds a row number, which in your example would simply be 1, 2, 3, 4, 5. Now, take the difference from the date, and you have a constant value for the group.

    select name, date,
           dateadd(d, - row_number() over (partition by name order by date), date) as val
    from t
    

    Finally, you want the number of groups in sequence. I would also add a group identifier (for instance, to distinguish between the last two).

    select name, date,
           count(*) over (partition by name, val) as NumInSeq,
           dense_rank() over (partition by name order by val) as SeqID
    from (select name, date,
                 dateadd(d, - row_number() over (partition by name order by date), date) as val
          from t
         ) t
    

    Somehow, I missed the part about weekdays and holidays. This solution does not solve that problem.

提交回复
热议问题