I have a table that contains following data:
It has balance containing 0s and 1s. Required is the total number of consecutive 0s as well the count of consecutive instances, as below.
I have a table that contains following data:
It has balance containing 0s and 1s. Required is the total number of consecutive 0s as well the count of consecutive instances, as below.
You can assign a grouping to the 0
s by doing a cumulative sum of 1
s. The rest is aggregation. So, for each sequence of 0
s:
select min(dt), max(dt), count(*)
from (select t.*,
sum(bal) over (order by dt) as grp
from t
) t
where bal = 0
group by grp;
I'm not sure how you want to summarize this, but this solves the "hard" part of the problem.