so I'm trying to implement gaps and islands problem over a set of numbers, here's an example:
0 0 3 4 5 6 6 7 7 8 11 12 18 22
That's a cumulative sum, so the numbers can not decrease. What I need to do is to separate respective records into groups by these rules:
- The first (and the smallest) number in a group is a "leading" number
- A number can be in the same group as its' leading number only if it's larger by no more than a certain number (let's say 7 for this example)
- The first number to exceed
leading number + 7
is a leading number for the next group
So with the example shown above, the groups would be:
0 0 3 4 5 6 6 7 7
8 11 12
18 22
It's almost like dividing a number by the gap, and getting a group number that way, but since a gap between the last number in a group and the leading number in the next group can be any positive number, this would get more and more incorrect as the sequence grows. I tried case when sum(...) over(...) > 7 then sum(...) else 0 end
but it affects every single number that's not in the first group, so I'm not sure how to approach this anymore. Any help would be appreciated! In case that's important, the table is in Snowflake