데이터는 아래처럼 되어있구요. START 이후 END 시점에 ON 인지 OFF 인지 알고 싶거든요.
데이터는 첫번째 표처럼 검은색으로만 되어 있지만 사실 구간사이에 빨간색 ON OFF 가 생략이 되어 있는 겁니다.
100만건정도 되는데 어떻게 짜야할지 감이 안잡히네요. 부탁드립니다.
ON |
START |
END |
OFF |
START |
ON |
END |
START |
ON |
OFF |
ON |
START |
END |
ON |
OFF |
START |
ON |
END |
ON |
START |
ON |
OFF |
일단 순서 값이 있어야 편할듯 합니다.
with
data as
(
select 1 no,'ON' gb union all
select 2,'START' gb union all
select 3,'END' gb union all
select 4,'OFF' gb union all
select 5,'START' gb union all
select 6,'ON' gb union all
select 7,'END' gb union all
select 8,'START' gb union all
select 9,'ON' gb union all
select 10,'OFF' gb ) ,
res as (
select no , gb , row_number() over (order by no) nno
from data a
where gb <> 'START' )
select *
from ( select * , 1 nno
from data
union all
select a.no, b.gb , 2 nno
from res a join res b on a.nno = b.nno + 1
where a.gb = 'END' ) r
order by r.no,r.nno