with a as
(
select 'A' as CODE, '10' AS STARTTIME, '20' AS ENDTIME from dual
union all
select 'B' as CODE, '20' AS STARTTIME, '30' AS ENDTIME from dual
union all
select 'C' as CODE, '30' AS STARTTIME, '40' AS ENDTIME from dual
union all
select 'D' as CODE, '40' AS STARTTIME, '50' AS ENDTIME from dual
union all
select 'E' as CODE, '50' AS STARTTIME, '100' AS ENDTIME from dual
)
select * from a;
CODE |
STARTTIME |
ENDTIME |
A |
5 |
20 |
B |
20 |
30 |
C |
30 |
40 |
D |
40 |
50 |
E |
50 |
100 |
CUTOFF의 조건
STARTTIME : 10
ENDTIME : 60
A의 경우 START는 5이지만 CUTOFF가 10 이기 때문에
시작이 10 으로 나와야 하며
E의 경우 END가 100 이지만 CUTOFF가 60 이기 때문에
다음 행으로 넘어갑니다.
[결과]
CODE |
STARTTIME |
ENDTIME |
A |
10 |
20 |
B |
20 |
30 |
C |
30 |
40 |
D |
40 |
50 |
E |
50 |
60 |
|
60 |
100 |
WITH A AS ( SELECT 'A' AS code, '10' AS starttime, '20' AS endtime FROM DUAL UNION ALL SELECT 'B', '20', '30' FROM DUAL UNION ALL SELECT 'C', '30', '40' FROM DUAL UNION ALL SELECT 'D', '40', '50' FROM DUAL UNION ALL SELECT 'E', '50', '100' FROM DUAL ) SELECT code, CASE WHEN(TO_NUMBER(LAG(MAX(endtime)) OVER(ORDER BY CODE)) > 60) THEN '60' ELSE MAX(starttime) END AS starttime, CASE WHEN(TO_NUMBER(MAX(endtime)) > 60) THEN '60' ELSE DECODE(CODE, NULL, LAG(MAX(ENDTIME))OVER(ORDER BY CODE), MAX(endtime)) END AS endtime FROM A GROUP BY ROLLUP(code) ; --참고만 하세요, 모범 답안은 곧 올라 올 것 같습니다. ^^;
WITH a AS ( SELECT 'A' code, 5 starttime, 20 endtime FROM dual UNION ALL SELECT 'B', 20, 30 FROM dual UNION ALL SELECT 'C', 30, 40 FROM dual UNION ALL SELECT 'D', 40, 50 FROM dual UNION ALL SELECT 'E', 50, 100 FROM dual ) SELECT code , DECODE(lv, 2, b.e, GREATEST(b.s, a.starttime)) starttime , DECODE(lv, 2, a.endtime, LEAST(b.e, a.endtime)) endtime FROM a , (SELECT 10 s, 60 e FROM dual) b , (SELECT LEVEL lv FROM dual CONNECT BY LEVEL <= 2) c WHERE b.e >= a.starttime AND b.s <= a.endtime AND c.lv <= CASE WHEN b.e < a.endtime THEN 2 ELSE 1 END ;