Пожалуйста, проверьте это решение ( fiddle ):
with cte(id, value, ts) as (
select 123, 'T' , 'ts1'
union all
select 123, 'T' , 'ts2'
union all
select 123, 'F' , 'ts3'
union all
select 123, 'T' , 'ts4'
union all
select 456, 'F' , 'ts5'
union all
select 456, 'T' , 'ts6'
union all
select 456, 'T' , 'ts7'
union all
select 456, 'F' , 'ts8'
)
select cnt as num_of_consecutives, count(cnt) as times_of_occurrences_for_this_number_of_consecutives from(
select value, count(*) cnt from(
select *,row_number() over (order by ts) - row_number() over (partition by value order by ts) grp
from cte)q
group by grp, value
)q1
where value = 'T'
group by value, cnt
order by cnt;
Это обсуждение также может быть полезно.