안녕하세요 쿼리를 짜다가 어떻게 해야할지 몰라 질문드립니다.
WITH T1 AS ( SELECT '1' KEY, '' PARENT_KEY, '정보' TYPE, '2020-09-30' APDY, '2020-10-16' ENDY FROM DUAL UNION ALL SELECT '2', '', '정보', '2020-10-17', '9999-12-31' FROM DUAL ) , T2 AS( SELECT '1' KEY, '' PARENT_KEY, 'D' TYPE, '2020-11-01' APDY, '2020-11-01' ENDY FROM DUAL UNION ALL SELECT '2', '', 'A', '2020-11-23', '2020-11-23' FROM DUAL UNION ALL SELECT '3', '', 'C', '2020-12-02', '2020-12-02' FROM DUAL UNION ALL SELECT '4', '', 'D', '2020-12-15', '2020-12-15' FROM DUAL UNION ALL SELECT '1', '', 'A', '2020-10-06', '2020-10-06' FROM DUAL UNION ALL SELECT '2', '', 'B', '2020-10-12', '2020-10-12' FROM DUAL UNION ALL SELECT '3', '', 'C', '2020-10-15', '2020-10-15' FROM DUAL ) SELECT KEY, PARENT_KEY, TYPE, APDY, ENDY FROM T1 UNION ALL SELECT KEY, PARENT_KEY, TYPE, APDY, ENDY FROM T2 ORDER BY APDY
테스트 데이터 입니다.
PARENT_KEY를 채워야하는데 어떻게 그 값을 도출해야 할 지 몰라서 질문드립니다...
계층의 LEVEL1은 TYPE컬럼이 '정보'라고 되어있는것이고, LEVEL2는 A,B,C 등등 으로 구성되어있습니다.
LEVEL2들의 APDY와 ENDY는 무조건 LEVEL1의 APDY와 ENDY사이에 들어갑니다.
결과는 첨부해놓은 사진과 같아야합니다.
정리하자면, 맨위의 테스트쿼리를 첨부한 사진처럼 어떻게 뽑아내나요..?
참고로 계층은 LEVEL2까지밖에 없습니다.
WITH t1 AS ( SELECT '1' key, '정보' type, '2020-09-30' apdy, '2020-10-16' endy FROM dual UNION ALL SELECT '2', '정보', '2020-10-17', '9999-12-31' FROM dual ) , t2 AS ( SELECT '1' key, 'D' type, '2020-11-01' apdy, '2020-11-01' endy FROM dual UNION ALL SELECT '2', 'A', '2020-11-23', '2020-11-23' FROM dual UNION ALL SELECT '3', 'C', '2020-12-02', '2020-12-02' FROM dual UNION ALL SELECT '4', 'D', '2020-12-15', '2020-12-15' FROM dual UNION ALL SELECT '1', 'A', '2020-10-06', '2020-10-06' FROM dual UNION ALL SELECT '2', 'B', '2020-10-12', '2020-10-12' FROM dual UNION ALL SELECT '3', 'C', '2020-10-15', '2020-10-15' FROM dual ) SELECT key , null parent_key , type , apdy , endy , 1 lv FROM t1 UNION ALL SELECT c.key , p.key parent_key , c.type , c.apdy , c.endy , 2 lv FROM t1 p , t2 c WHERE c.apdy >= p.apdy AND c.endy <= p.endy ORDER BY apdy, lv ;