for a given date I want to get the streak (dates backward in a row where number is above/equals a certain threshold => in the examples always 1000)
Examples:
- date input 2024-11-15 => Result 2
- date input
2024-11-14 => Result 1 - date input 2024-11-8 => Result 0
- date input 2024-11-7 => Result 3
- date input
2024-11-5 => Result 1
I would greatly appreciate your help. Thank you so much!
2
Answers
Most simple is to use recursivity :
Streak
CTE will assign a row_number to all the dates where number is >= 1000.SELF JOINED
to find the dates which are consecutive and the number of days are counted based on difference of row numbers.Fiddle
Output