Here is one of the possible solutions.
(Note that for the simplicity of this case I deliberately made the data in the TIME column unique, so that we don't have a unique sorting problem that DaPi is refering to in the previous post.)
Code:SQL> select * from tab1 order by time; TIME SUBTYPE VALUE -------- ------- ---------- 07:00:00 a 2 07:00:20 a 0 07:00:40 a 5 07:03:00 a 3 07:03:20 a 2 07:03:40 a 1 07:06:00 a 3 07:06:20 a 3 07:06:40 a 4 07:09:00 b 5 07:09:20 b 2 07:09:40 b 1 12 rows selected. SQL> select 2 time, subtype, value, 3 case when preceding = 3 or following = 3 then 'y' end as flag 4 from (select 5 time, subtype, value, indicator3, rownum, 6 count(indicator3) over(order by rownum rows between 2 preceding and current row) as preceding, 7 count(indicator3) over(order by rownum rows between current row and 2 following) as following 8 from (select 9 time, subtype, value, 10 case when value >= 3 then 1 end as indicator3 11 from tab1 12 order by time 13 ) 14 ); TIME SUBTYPE VALUE FLAG -------- ------- ---------- ---- 07:00:00 a 2 07:00:20 a 0 07:00:40 a 5 07:03:00 a 3 07:03:20 a 2 07:03:40 a 1 07:06:00 a 3 y 07:06:20 a 3 y 07:06:40 a 4 y 07:09:00 b 5 y 07:09:20 b 2 07:09:40 b 1 12 rows selected. SQL>




Reply With Quote