I really hope someone can show us if this is possible using the straightforward window functions. This is a real problem.
In the meantime, this is how I will do this with recursion. This processes the spaces in the sequence and processes the edge edge of the first line already having >= 10 .
I also added a maxrecursion hint to remove the default recursion limit. But I honestly don’t know how well it will work with large amounts of data.
with NumberedRows as ( select Id, Cnt, row_number() over(order by id) as rn from CountTable ), RecursiveCTE as ( select Id, Cnt, rn, case when Cnt >= 10 then 0 else Cnt end as CumulativeSum, case when Cnt >= 10 then 'Y' else 'N' end as hit from NumberedRows where rn = 1 union all select n.Id, n.Cnt, n.rn, case when (n.Cnt + r.CumulativeSum) >= 10 then 0 else n.Cnt + r.CumulativeSum end as CumulativeSum, case when (n.Cnt + r.CumulativeSum) >= 10 then 'Y' else 'N' end as hit from RecursiveCTE r join NumberedRows n on n.rn = r.rn + 1 ) select Id, Cnt, hit from RecursiveCTE order by Id option (maxrecursion 0)
SQLFiddle Demo
sstan source share