By using db<>fiddle, you agree to license everything you submit by Creative Commons CC0.
with tbl(ttime, temperature) as (
select 0 , 2 union all
select 1 , 1 union all
select 2 , 0 union all
select 3 , -1 union all
select 4 , -2 union all
select 5 , -4 union all
select 6 , -4 union all
select 7 , -5 union all
select 8 , -3 union all
select 9 , -2 union all
select 10 , -1 union all
select 11 , -1 union all
select 12 , 1 union all
select 13 , 1 union all
select 14 , -1 union all
select 15 , -2 union all
select 16 , -1 union all
select 17 , 0 union all
select 18 , 1
)
select ttime, temperature, case when min(temperature) over(partition by grp) =-5 then 1 else 0 end flag
from (
select *, row_number() over(order by ttime) - row_number() over(partition by case when temperature<0 then 1 else 2 end order by ttime) grp
from tbl
) t
order by ttime
ttime | temperature | flag |
---|---|---|
0 | 2 | 0 |
1 | 1 | 0 |
2 | 0 | 0 |
3 | -1 | 1 |
4 | -2 | 1 |
5 | -4 | 1 |
6 | -4 | 1 |
7 | -5 | 1 |
8 | -3 | 1 |
9 | -2 | 1 |
10 | -1 | 1 |
11 | -1 | 1 |
12 | 1 | 0 |
13 | 1 | 0 |
14 | -1 | 0 |
15 | -2 | 0 |
16 | -1 | 0 |
17 | 0 | 0 |
18 | 1 | 0 |