table
| customer id | login |
|---|---|
| 1 | 2016-03-01 |
| 1 | 2016-03-02 |
| 1 | 2016-03-03 |
| 1 | 2016-03-05 |
| 1 | 2016-03-06 |
| 1 | 2016-03-07 |
| 1 | 2016-03-08 |
I want to find what is the maximum number of times the customer id logged in consecutively.
My approach is
Use self join to create lag (I use self join because directly using lead and lag will not work here).
Now somehow I need to create a column which checks if previous date is null then 0, else count 1,2,3,..... If previous date is again 0 then start counting from zero again.
| customer id | login | previous date | consecutive_day_flag |
|---|---|---|---|
| 1 | 2016-03-01 | null | 0 |
| 1 | 2016-03-02 | 2016-03-01 | 1 |
| 1 | 2016-03-03 | 2016-03-02 | 2 |
| 1 | 2016-03-05 | null | 0 |
| 1 | 2016-03-06 | 2016-03-05 | 1 |
| 1 | 2016-03-07 | 2016-03-06 | 2 |
| 1 | 2016-03-08 | 2016-03-08 | 3 |
-- Now I can find the max consecutive_day_flag and name the column as highest consecutive days.
| highest_consecutive_day |
|---|
| 3 |
How can I handle the step 2 of creating a consecutive_day_flag column?
Or please suggest any better approach.
I have tried above approach.
This is a gaps and islands problem. One method for solving it uses
LEAD()to get next row, then useROW_NUMBER()andSUM()to generate groups :Result :
Demo here