Table Customer
| ID | Action | Date | Time | 
|---|---|---|---|
| 10 | Deal | 26.04.2023 | 15:00 | 
| 10 | Meet | 25.04.2023 | 14:15 | 
| 10 | Call | 24.04.2023 | 13:00 | 
| 10 | Wait | 23.04.2023 | 12:00 | 
| 201 | Wait | 15.04.2023 | 14:30 | 
| 201 | Wait | 15.04.2023 | 13:30 | 
| 201 | Call | 15.04.2023 | 12:30 | 
| 201 | Call | 15.04.2023 | 11:30 | 
| 201 | Wait | 15.04.2023 | 10:30 | 
| 3020 | Wait | 6.10.2021 | 8:00 | 
| 3020 | Wait | 5.10.2021 | 8:00 | 
| 3020 | Call | 4.10.2021 | 8:00 | 
| 3020 | Wait | 2.10.2021 | 8:00 | 
| 3020 | Wait | 1.10.2021 | 8:00 | 
| 3020 | Wait | 1.10.2021 | 8:00 | 
I need to find out a wait-streak for each ID.
Definition for wait-streak is:
- Starting from ID's newest row, ifActionisWaitthen wait-streak = 1
- If next chronological row with same IDhasAction=Wait, wait-streak is added by 1
- All rows for IDare chronologically looped and wait-streak is added by 1 if streak continues
- If there's are multiple streaks of Wait, biggest must be found and value of newest streak must be preserved
In addition to Date, also Time must be  taken into account. Is it better to combine these into a new date/time column or is there a simple way to use existing columns?
After or during the calculation, results should be updated into a new table. Longest found streak into column BiggestWaitStreak, latest streak into column NewestWaitStreak and if newest Action is Wait then column NewestIsWait must be set to 1.
After Update new statistics table should look like:
Table ContactStats
| ID | BiggestWaitStreak | NewestWaitStreak | NewestIsWait | 
|---|---|---|---|
| 10 | 1 | 1 | 0 | 
| 201 | 2 | 2 | 1 | 
| 3020 | 3 | 2 | 1 | 
I guess best way to achieve this would be utilizing CTE or temporary tables. Personally I prefer CTE. I would be happy to add my work in progress but unfortunately there is none. I don't know where to start.
Any help a highly appreciated.
 
    