by 홍길덩이 [SQL Query] 오라클 [2019.03.26 15:46:35]
저번에 질문 답변해 주신 마농님 감사합니다.
이전 질문에서 좀더 내용 추가하여 요청 드립니다.
제가 정렬하고 싶은방법은 작업일자가 가장 빠른순 > 작업변경시간 빠른순 > 용량 많은순 (DESC) 이며,
추가로 이전 라인에 같은 라인으로 연결하여 만들고 싶은데,
결과화면과 같이 1라인 (1번)-> 1라인(2번) -> 2라인(1번) -> 1라인 (3번) -> 2라인(2번)
이런식으로 그전 아이템과 아이템 변경시간 빠른순으로 가져와서 정렬 하고 싶습니다.
FROM_ITEM | TO_ITEM | 아이템변경시간 |
A | B | 5 |
A | F | 5 |
A | C | 5 |
A | D | 10 |
A | E | 10 |
A | G | 5 |
B | A | 10 |
B | F | 5 |
B | C | 5 |
B | D | 10 |
B | E | 5 |
B | G | 5 |
F | A | 5 |
F | B | 5 |
F | C | 5 |
F | D | 10 |
F | E | 20 |
F | G | 10 |
F | H | 5 |
E | A | 10 |
E | B | 10 |
E | C | 5 |
E | D | 10 |
E | F | 10 |
E | G | 5 |
C | A | 5 |
C | B | 5 |
C | E | 5 |
C | D | 5 |
C | F | 10 |
C | G | 10 |
G | A | 5 |
G | B | 10 |
G | E | 10 |
G | D | 5 |
G | F | 5 |
G | C | 10 |
D | A | 10 |
D | B | 5 |
D | E | 5 |
D | G | 10 |
D | F | 10 |
D | C | 10 |
-- 작업관리리스트
아이템 | 작업날짜 | 라인 | 용량 |
A | 2019-03-23 0:00 | 2 | 18 |
E | 2019-03-23 0:00 | 2 | 5 |
C | 2019-03-24 0:00 | 1 | 15 |
F | 2019-03-24 0:00 | 2 | 11 |
D | 2019-03-24 0:00 | 1 | 10 |
G | 2019-03-24 0:00 | 1 | 13 |
B | 2019-03-24 0:00 | 1 | 6 |
H | 2019-03-26 0:00 | 2 | 2 |
-- 결과화면
ITEM_CODE | 작업날짜 | 변경아이템 | 변경시간 | 변경아이템 용량 | 라인 |
A | 2019-03-23 0:00 | E | 10 | 5 | 2 |
E | 2019-03-23 0:00 | F | 10 | 11 | 2 |
C | 2019-03-24 0:00 | D | 5 | 10 | 1 |
D | 2019-03-24 0:00 | B | 5 | 6 | 1 |
G | 2019-03-24 0:00 | 1 | |||
F | 2019-03-24 0:00 | H | 5 | 2 | 2 |
H | 2019-03-26 0:00 | 2 |
WITH W_NAME AS ( SELECT 'A' 아이템, TO_DATE('20190323', 'yyyymmdd') 납품일, 2 라인, 18 용량 FROM DUAL UNION ALL SELECT 'E', TO_DATE('20190323', 'yyyymmdd'), 2 라인, 5 용량 FROM DUAL UNION ALL SELECT 'C', TO_DATE('20190324', 'yyyymmdd'), 1 라인, 15 용량 FROM DUAL UNION ALL SELECT 'F', TO_DATE('20190324', 'yyyymmdd'), 2 라인, 11 용량 FROM DUAL UNION ALL SELECT 'D', TO_DATE('20190324', 'yyyymmdd'), 1 라인, 10 용량 FROM DUAL UNION ALL SELECT 'G', TO_DATE('20190324', 'yyyymmdd'), 1 라인, 13 용량 FROM DUAL UNION ALL SELECT 'B', TO_DATE('20190324', 'yyyymmdd'), 1 라인, 6 용량 FROM DUAL UNION ALL SELECT 'H', TO_DATE('20190326', 'yyyymmdd'), 2 라인, 2 용량 FROM DUAL ) , W_TIME AS ( SELECT 'A' AS FROM_ITEM, 'B' AS TO_ITEM, 5 AS "아이템변경시간" FROM DUAL UNION ALL SELECT 'A' AS FROM_ITEM, 'F' AS TO_ITEM, 5 AS "아이템변경시간" FROM DUAL UNION ALL SELECT 'A' AS FROM_ITEM, 'C' AS TO_ITEM, 10 AS "아이템변경시간" FROM DUAL UNION ALL SELECT 'A' AS FROM_ITEM, 'D' AS TO_ITEM, 10 AS "아이템변경시간" FROM DUAL UNION ALL SELECT 'A' AS FROM_ITEM, 'E' AS TO_ITEM, 10 AS "아이템변경시간" FROM DUAL UNION ALL SELECT 'A' AS FROM_ITEM, 'G' AS TO_ITEM, 5 AS "아이템변경시간" FROM DUAL UNION ALL SELECT 'B' AS FROM_ITEM, 'A' AS TO_ITEM, 10 AS "아이템변경시간" FROM DUAL UNION ALL SELECT 'B' AS FROM_ITEM, 'F' AS TO_ITEM, 5 AS "아이템변경시간" FROM DUAL UNION ALL SELECT 'B' AS FROM_ITEM, 'C' AS TO_ITEM, 5 AS "아이템변경시간" FROM DUAL UNION ALL SELECT 'B' AS FROM_ITEM, 'D' AS TO_ITEM, 10 AS "아이템변경시간" FROM DUAL UNION ALL SELECT 'B' AS FROM_ITEM, 'E' AS TO_ITEM, 5 AS "아이템변경시간" FROM DUAL UNION ALL SELECT 'B' AS FROM_ITEM, 'G' AS TO_ITEM, 5 AS "아이템변경시간" FROM DUAL UNION ALL SELECT 'F' AS FROM_ITEM, 'A' AS TO_ITEM, 5 AS "아이템변경시간" FROM DUAL UNION ALL SELECT 'F' AS FROM_ITEM, 'B' AS TO_ITEM, 5 AS "아이템변경시간" FROM DUAL UNION ALL SELECT 'F' AS FROM_ITEM, 'C' AS TO_ITEM, 5 AS "아이템변경시간" FROM DUAL UNION ALL SELECT 'F' AS FROM_ITEM, 'D' AS TO_ITEM, 10 AS "아이템변경시간" FROM DUAL UNION ALL SELECT 'F' AS FROM_ITEM, 'E' AS TO_ITEM, 20 AS "아이템변경시간" FROM DUAL UNION ALL SELECT 'F' AS FROM_ITEM, 'G' AS TO_ITEM, 10 AS "아이템변경시간" FROM DUAL UNION ALL SELECT 'F' AS FROM_ITEM, 'H' AS TO_ITEM, 5 AS "아이템변경시간" FROM DUAL UNION ALL SELECT 'E' AS FROM_ITEM, 'A' AS TO_ITEM, 10 AS "아이템변경시간" FROM DUAL UNION ALL SELECT 'E' AS FROM_ITEM, 'B' AS TO_ITEM, 10 AS "아이템변경시간" FROM DUAL UNION ALL SELECT 'E' AS FROM_ITEM, 'C' AS TO_ITEM, 5 AS "아이템변경시간" FROM DUAL UNION ALL SELECT 'E' AS FROM_ITEM, 'D' AS TO_ITEM, 10 AS "아이템변경시간" FROM DUAL UNION ALL SELECT 'E' AS FROM_ITEM, 'F' AS TO_ITEM, 10 AS "아이템변경시간" FROM DUAL UNION ALL SELECT 'E' AS FROM_ITEM, 'G' AS TO_ITEM, 5 AS "아이템변경시간" FROM DUAL UNION ALL SELECT 'C' AS FROM_ITEM, 'A' AS TO_ITEM, 5 AS "아이템변경시간" FROM DUAL UNION ALL SELECT 'C' AS FROM_ITEM, 'B' AS TO_ITEM, 5 AS "아이템변경시간" FROM DUAL UNION ALL SELECT 'C' AS FROM_ITEM, 'E' AS TO_ITEM, 5 AS "아이템변경시간" FROM DUAL UNION ALL SELECT 'C' AS FROM_ITEM, 'D' AS TO_ITEM, 5 AS "아이템변경시간" FROM DUAL UNION ALL SELECT 'C' AS FROM_ITEM, 'F' AS TO_ITEM, 10 AS "아이템변경시간" FROM DUAL UNION ALL SELECT 'C' AS FROM_ITEM, 'G' AS TO_ITEM, 10 AS "아이템변경시간" FROM DUAL UNION ALL SELECT 'G' AS FROM_ITEM, 'A' AS TO_ITEM, 5 AS "아이템변경시간" FROM DUAL UNION ALL SELECT 'G' AS FROM_ITEM, 'B' AS TO_ITEM, 10 AS "아이템변경시간" FROM DUAL UNION ALL SELECT 'G' AS FROM_ITEM, 'E' AS TO_ITEM, 10 AS "아이템변경시간" FROM DUAL UNION ALL SELECT 'G' AS FROM_ITEM, 'D' AS TO_ITEM, 5 AS "아이템변경시간" FROM DUAL UNION ALL SELECT 'G' AS FROM_ITEM, 'F' AS TO_ITEM, 5 AS "아이템변경시간" FROM DUAL UNION ALL SELECT 'G' AS FROM_ITEM, 'C' AS TO_ITEM, 10 AS "아이템변경시간" FROM DUAL UNION ALL SELECT 'D' AS FROM_ITEM, 'A' AS TO_ITEM, 10 AS "아이템변경시간" FROM DUAL UNION ALL SELECT 'D' AS FROM_ITEM, 'B' AS TO_ITEM, 5 AS "아이템변경시간" FROM DUAL UNION ALL SELECT 'D' AS FROM_ITEM, 'E' AS TO_ITEM, 5 AS "아이템변경시간" FROM DUAL UNION ALL SELECT 'D' AS FROM_ITEM, 'G' AS TO_ITEM, 10 AS "아이템변경시간" FROM DUAL UNION ALL SELECT 'D' AS FROM_ITEM, 'F' AS TO_ITEM, 10 AS "아이템변경시간" FROM DUAL UNION ALL SELECT 'D' AS FROM_ITEM, 'C' AS TO_ITEM, 10 AS "아이템변경시간" FROM DUAL )
WITH w_name AS ( SELECT 'A' item_code, TO_DATE('20190323', 'yyyymmdd') 작업날짜, 2 라인, 18 용량 FROM dual UNION ALL SELECT 'E', TO_DATE('20190323', 'yyyymmdd'), 2 라인, 5 용량 FROM dual UNION ALL SELECT 'C', TO_DATE('20190324', 'yyyymmdd'), 1 라인, 15 용량 FROM dual UNION ALL SELECT 'F', TO_DATE('20190324', 'yyyymmdd'), 2 라인, 11 용량 FROM dual UNION ALL SELECT 'D', TO_DATE('20190324', 'yyyymmdd'), 1 라인, 10 용량 FROM dual UNION ALL SELECT 'G', TO_DATE('20190324', 'yyyymmdd'), 1 라인, 13 용량 FROM dual UNION ALL SELECT 'B', TO_DATE('20190324', 'yyyymmdd'), 1 라인, 6 용량 FROM dual UNION ALL SELECT 'H', TO_DATE('20190326', 'yyyymmdd'), 2 라인, 2 용량 FROM dual ) , w_time AS ( SELECT 'A' from_item, 'B' to_item, 5 아이템변경시간 FROM dual UNION ALL SELECT 'A', 'F', 5 FROM dual UNION ALL SELECT 'A', 'C', 10 FROM dual UNION ALL SELECT 'A', 'D', 10 FROM dual UNION ALL SELECT 'A', 'E', 10 FROM dual UNION ALL SELECT 'A', 'G', 5 FROM dual UNION ALL SELECT 'B', 'A', 10 FROM dual UNION ALL SELECT 'B', 'F', 5 FROM dual UNION ALL SELECT 'B', 'C', 5 FROM dual UNION ALL SELECT 'B', 'D', 10 FROM dual UNION ALL SELECT 'B', 'E', 5 FROM dual UNION ALL SELECT 'B', 'G', 5 FROM dual UNION ALL SELECT 'F', 'A', 5 FROM dual UNION ALL SELECT 'F', 'B', 5 FROM dual UNION ALL SELECT 'F', 'C', 5 FROM dual UNION ALL SELECT 'F', 'D', 10 FROM dual UNION ALL SELECT 'F', 'E', 20 FROM dual UNION ALL SELECT 'F', 'G', 10 FROM dual UNION ALL SELECT 'F', 'H', 5 FROM dual UNION ALL SELECT 'E', 'A', 10 FROM dual UNION ALL SELECT 'E', 'B', 10 FROM dual UNION ALL SELECT 'E', 'C', 5 FROM dual UNION ALL SELECT 'E', 'D', 10 FROM dual UNION ALL SELECT 'E', 'F', 10 FROM dual UNION ALL SELECT 'E', 'G', 5 FROM dual UNION ALL SELECT 'C', 'A', 5 FROM dual UNION ALL SELECT 'C', 'B', 5 FROM dual UNION ALL SELECT 'C', 'E', 5 FROM dual UNION ALL SELECT 'C', 'D', 5 FROM dual UNION ALL SELECT 'C', 'F', 10 FROM dual UNION ALL SELECT 'C', 'G', 10 FROM dual UNION ALL SELECT 'G', 'A', 5 FROM dual UNION ALL SELECT 'G', 'B', 10 FROM dual UNION ALL SELECT 'G', 'E', 10 FROM dual UNION ALL SELECT 'G', 'D', 5 FROM dual UNION ALL SELECT 'G', 'F', 5 FROM dual UNION ALL SELECT 'G', 'C', 10 FROM dual UNION ALL SELECT 'D', 'A', 10 FROM dual UNION ALL SELECT 'D', 'B', 5 FROM dual UNION ALL SELECT 'D', 'E', 5 FROM dual UNION ALL SELECT 'D', 'G', 10 FROM dual UNION ALL SELECT 'D', 'F', 10 FROM dual UNION ALL SELECT 'D', 'C', 10 FROM dual ) , w_temp(item_path, lv, item_code, 작업날짜, 용량, 라인, to_item) AS ( SELECT CAST('-' || item_code AS VARCHAR2(99)) item_path , 1 lv , item_code , 작업날짜 , 용량 , 라인 , (SELECT MIN(b.to_item) KEEP(DENSE_RANK FIRST ORDER BY c.작업날짜, b.아이템변경시간, c.용량 DESC, b.to_item) FROM w_time b , w_name c WHERE b.from_item = a.item_code AND c.item_code = b.to_item AND c.라인 = a.라인 ) to_item FROM w_name a WHERE item_code IN (SELECT MIN(item_code) KEEP(DENSE_RANK FIRST ORDER BY 작업날짜, 용량 DESC, item_code) FROM w_name GROUP BY 라인 ) UNION ALL SELECT d.item_path || '-' || a.item_code item_path , d.lv + 1 lv , a.item_code , a.작업날짜 , a.용량 , a.라인 , (SELECT MIN(b.to_item) KEEP(DENSE_RANK FIRST ORDER BY c.작업날짜, b.아이템변경시간, c.용량 DESC, b.to_item) FROM w_time b , w_name c WHERE b.from_item = a.item_code AND c.item_code = b.to_item AND c.라인 = a.라인 AND INSTR(d.item_path||'-', '-'||b.to_item||'-') = 0 ) to_item FROM w_name a , w_temp d WHERE a.item_code = d.to_item ) SELECT a.item_code , a.작업날짜 , a.to_item , b.아이템변경시간 , LEAD(a.용량) OVER(PARTITION BY a.라인 ORDER BY a.lv) 변경아이템용량 , a.라인 , a.lv FROM w_temp a , w_time b WHERE a.item_code = b.from_item(+) AND a.to_item = b.to_item(+) ;
혹시 동일한 아이템이 있을경우 같은 정렬조건에 추가 할 수 있을까요?
W_TIME에는 동일 아이템에 대한 조건이 없어서 -> 'A' FROM_ITEM 'A' TO_ITEM 0 아이템 변경시간
W_NAME에 동일 아이템이 있는경우엔 아이템변경시간을 0으로 주고
작업일자가 가장 빠른순 > 동일아이템 > 작업변경시간 빠른순 > 용량 많은순 (DESC)
이렇게 하는게 해보는데 잘 안나옵니다 ㅠㅠ
-- 작업관리리스트
아이템 | 작업날짜 | 라인 | 용량 |
A | 2019-03-23 0:00 | 2 | 18 |
E | 2019-03-23 0:00 | 2 | 5 |
C | 2019-03-24 0:00 | 1 | 15 |
F | 2019-03-24 0:00 | 2 | 11 |
D | 2019-03-24 0:00 | 1 | 10 |
G | 2019-03-24 0:00 | 1 | 13 |
B | 2019-03-24 0:00 | 1 | 6 |
H | 2019-03-26 0:00 | 2 | 2 |
A 2019-03-24 0:00 2 10
WITH w_name AS ( SELECT 'A' item_code, TO_DATE('20190323', 'yyyymmdd') 작업날짜, 2 라인, 18 용량 FROM dual UNION ALL SELECT 'E', TO_DATE('20190323', 'yyyymmdd'), 2 라인, 5 용량 FROM dual UNION ALL SELECT 'C', TO_DATE('20190324', 'yyyymmdd'), 1 라인, 15 용량 FROM dual UNION ALL SELECT 'F', TO_DATE('20190324', 'yyyymmdd'), 2 라인, 11 용량 FROM dual UNION ALL SELECT 'D', TO_DATE('20190324', 'yyyymmdd'), 1 라인, 10 용량 FROM dual UNION ALL SELECT 'G', TO_DATE('20190324', 'yyyymmdd'), 1 라인, 13 용량 FROM dual UNION ALL SELECT 'B', TO_DATE('20190324', 'yyyymmdd'), 1 라인, 6 용량 FROM dual UNION ALL SELECT 'H', TO_DATE('20190326', 'yyyymmdd'), 2 라인, 2 용량 FROM dual UNION ALL SELECT 'A', TO_DATE('20190326', 'yyyymmdd'), 2 라인, 10 용량 FROM dual )
결과화면에 동일한 날짜의 동일한 아이템일경우 우선적으로 순서를 더 높게 주고
W_NAME에 동일아이템은 W_TIME에 데이터가 없기 때문에 아이템변경시간은 0으로 하고
정렬 순서는
작업일자 가장 빠른순 > 아이템변경시간 빠른순(동일아이템은 아이템 변경시간 0) > 용량 많은순 (DESC)
으로 하고 싶습니다.
--W_TIME
FROM_ITEM | TO_ITEM | 아이템변경시간 |
A | B | 5 |
A | F | 5 |
A | C | 5 |
A | D | 10 |
A | E | 10 |
A | G | 5 |
B | A | 10 |
B | F | 5 |
B | C | 5 |
B | D | 10 |
B | E | 5 |
B | G | 5 |
F | A | 5 |
F | B | 5 |
F | C | 5 |
F | D | 10 |
F | E | 20 |
F | G | 10 |
F | H | 5 |
E | A | 10 |
E | B | 10 |
E | C | 5 |
E | D | 10 |
E | F | 10 |
E | G | 5 |
C | A | 5 |
C | B | 5 |
C | E | 5 |
C | D | 5 |
C | F | 10 |
C | G | 10 |
G | A | 5 |
G | B | 10 |
G | E | 10 |
G | D | 5 |
G | F | 5 |
G | C | 10 |
D | A | 10 |
D | B | 5 |
D | E | 5 |
D | G | 10 |
D | F | 10 |
D | C | 10 |
--W_NAME
아이템 | 작업날짜 | 라인 | 용량 |
A | 2019-03-23 0:00 | 2 | 18 |
E | 2019-03-23 0:00 | 2 | 10 |
A | 2019-03-23 0:00 | 2 | 5 |
C | 2019-03-24 0:00 | 1 | 15 |
F | 2019-03-24 0:00 | 2 | 11 |
D | 2019-03-24 0:00 | 1 | 10 |
G | 2019-03-24 0:00 | 1 | 13 |
B | 2019-03-24 0:00 | 1 | 6 |
H | 2019-03-26 0:00 | 2 | 2 |
--결과화면
ITEM_CODE | 작업날짜 | 변경아이템 | 변경시간 | 변경아이템 용량 | 라인 |
A | 2019-03-23 0:00 | A | 0 | 5 | 2 |
A | 2019-03-23 0:00 | E | 10 | 10 | 2 |
E | 2019-03-23 0:00 | F | 10 | 11 | 2 |
C | 2019-03-24 0:00 | D | 5 | 10 | 1 |
D | 2019-03-24 0:00 | B | 5 | 6 | 1 |
G | 2019-03-24 0:00 | 1 | |||
F | 2019-03-24 0:00 | H | 5 | 2 | 2 |
H | 2019-03-26 0:00 | 2 |
1. 결과에 B->G 는 빼먹었습니다.
ITEM_CODE |
작업날짜 | 변경아이템 | 변경시간 | 변경아이템 용량 | 라인 |
A | 2019-03-23 0:00 | A | 0 | 5 | 2 |
A | 2019-03-23 0:00 | E | 10 | 10 | 2 |
E | 2019-03-23 0:00 | F | 10 | 11 | 2 |
C | 2019-03-24 0:00 | D | 5 | 10 | 1 |
D | 2019-03-24 0:00 | B | 5 | 6 | 1 |
B | 2019-03-24 0:00 | G | 5 | 13 | 1 |
G | 2019-03-24 0:00 | 1 | |||
F | 2019-03-24 0:00 | H | 5 | 2 | 2 |
H | 2019-03-26 0:00 |
2 |
2. 같은 아이템인경우 작업날짜가 같지 않은 경우 A-E-A 갈 수 있지만
A 3/23
E 3/23
A 3/24
같은 아이템의 같은 작업날짜인경우 A->A->E 로 이어집니다.
A 3/23
A 3/23
E 3/24
WITH w_name AS ( SELECT 'A' item_code, TO_DATE('20190323', 'yyyymmdd') 작업날짜, 2 라인, 18 용량 FROM dual UNION ALL SELECT 'E', TO_DATE('20190323', 'yyyymmdd'), 2 라인, 10 용량 FROM dual UNION ALL SELECT 'C', TO_DATE('20190324', 'yyyymmdd'), 1 라인, 15 용량 FROM dual UNION ALL SELECT 'F', TO_DATE('20190324', 'yyyymmdd'), 2 라인, 11 용량 FROM dual UNION ALL SELECT 'D', TO_DATE('20190324', 'yyyymmdd'), 1 라인, 10 용량 FROM dual UNION ALL SELECT 'G', TO_DATE('20190324', 'yyyymmdd'), 1 라인, 13 용량 FROM dual UNION ALL SELECT 'B', TO_DATE('20190324', 'yyyymmdd'), 1 라인, 6 용량 FROM dual UNION ALL SELECT 'H', TO_DATE('20190326', 'yyyymmdd'), 2 라인, 2 용량 FROM dual UNION ALL SELECT 'A', TO_DATE('20190323', 'yyyymmdd'), 2 라인, 5 용량 FROM dual ) , w_time AS ( SELECT 'A' from_item, 'B' to_item, 5 아이템변경시간 FROM dual UNION ALL SELECT 'A', 'F', 5 FROM dual UNION ALL SELECT 'A', 'C', 10 FROM dual UNION ALL SELECT 'A', 'D', 10 FROM dual UNION ALL SELECT 'A', 'E', 10 FROM dual UNION ALL SELECT 'A', 'G', 5 FROM dual UNION ALL SELECT 'B', 'A', 10 FROM dual UNION ALL SELECT 'B', 'F', 5 FROM dual UNION ALL SELECT 'B', 'C', 5 FROM dual UNION ALL SELECT 'B', 'D', 10 FROM dual UNION ALL SELECT 'B', 'E', 5 FROM dual UNION ALL SELECT 'B', 'G', 5 FROM dual UNION ALL SELECT 'F', 'A', 5 FROM dual UNION ALL SELECT 'F', 'B', 5 FROM dual UNION ALL SELECT 'F', 'C', 5 FROM dual UNION ALL SELECT 'F', 'D', 10 FROM dual UNION ALL SELECT 'F', 'E', 20 FROM dual UNION ALL SELECT 'F', 'G', 10 FROM dual UNION ALL SELECT 'F', 'H', 5 FROM dual UNION ALL SELECT 'E', 'A', 10 FROM dual UNION ALL SELECT 'E', 'B', 10 FROM dual UNION ALL SELECT 'E', 'C', 5 FROM dual UNION ALL SELECT 'E', 'D', 10 FROM dual UNION ALL SELECT 'E', 'F', 10 FROM dual UNION ALL SELECT 'E', 'G', 5 FROM dual UNION ALL SELECT 'C', 'A', 5 FROM dual UNION ALL SELECT 'C', 'B', 5 FROM dual UNION ALL SELECT 'C', 'E', 5 FROM dual UNION ALL SELECT 'C', 'D', 5 FROM dual UNION ALL SELECT 'C', 'F', 10 FROM dual UNION ALL SELECT 'C', 'G', 10 FROM dual UNION ALL SELECT 'G', 'A', 5 FROM dual UNION ALL SELECT 'G', 'B', 10 FROM dual UNION ALL SELECT 'G', 'E', 10 FROM dual UNION ALL SELECT 'G', 'D', 5 FROM dual UNION ALL SELECT 'G', 'F', 5 FROM dual UNION ALL SELECT 'G', 'C', 10 FROM dual UNION ALL SELECT 'D', 'A', 10 FROM dual UNION ALL SELECT 'D', 'B', 5 FROM dual UNION ALL SELECT 'D', 'E', 5 FROM dual UNION ALL SELECT 'D', 'G', 10 FROM dual UNION ALL SELECT 'D', 'F', 10 FROM dual UNION ALL SELECT 'D', 'C', 10 FROM dual ) , w_name1 AS ( SELECT item_code , ROW_NUMBER() OVER(PARTITION BY 라인 ORDER BY 작업날짜, 용량 DESC, item_code) rn1 , ROW_NUMBER() OVER(PARTITION BY item_code ORDER BY 작업날짜, 용량 DESC) rn , 작업날짜, 라인, 용량 FROM w_name ) , w_time1 AS ( SELECT b.item_code from_item , b.rn from_rn , c.라인 , c.item_code to_item , c.rn to_rn , c.작업날짜 , 0 아이템변경시간 , c.용량 FROM w_name1 b , w_name1 c WHERE b.item_code = c.item_code AND b.rn + 1 = c.rn UNION ALL SELECT a.from_item , b.rn from_rn , c.라인 , a.to_item , c.rn to_rn , c.작업날짜 , a.아이템변경시간 , c.용량 FROM w_time a , w_name1 b , w_name1 c WHERE a.from_item = b.item_code AND a.to_item = c.item_code ) , w_temp1(item_path, lv, item_code, rn, 작업날짜, 용량, 라인, to_item_rn) AS ( SELECT CAST('-'|| item_code ||'_'|| rn AS VARCHAR2(99)) item_path , 1 lv , item_code , rn , 작업날짜 , 용량 , 라인 , (SELECT MIN(b.to_item ||'_'|| b.to_rn) KEEP(DENSE_RANK FIRST ORDER BY c.작업날짜, b.아이템변경시간, c.용량 DESC, b.to_item) FROM w_time1 b , w_name1 c WHERE b.from_item = a.item_code AND b.from_rn = a.rn AND b.to_item = c.item_code AND b.to_rn = c.rn AND c.라인 = a.라인 ) to_item_rn FROM w_name1 a WHERE rn1 = 1 UNION ALL SELECT d.item_path ||'-'|| a.item_code ||'_'|| a.rn item_path , d.lv + 1 lv , a.item_code , a.rn , a.작업날짜 , a.용량 , a.라인 , (SELECT MIN(b.to_item ||'_'|| b.to_rn) KEEP(DENSE_RANK FIRST ORDER BY c.작업날짜, b.아이템변경시간, c.용량 DESC, b.to_item) FROM w_time1 b , w_name1 c WHERE b.from_item = a.item_code AND b.from_rn = a.rn AND b.to_item = c.item_code AND b.to_rn = c.rn AND c.라인 = a.라인 AND INSTR(d.item_path||'-', '-'||b.to_item||'_'||c.rn||'-') = 0 ) to_item_rn FROM w_name1 a , w_temp1 d WHERE a.라인 = d.라인 AND a.item_code||'_'||a.rn = d.to_item_rn AND INSTR(d.item_path||'-', '-'||a.item_code||'_'||a.rn||'-') = 0 ) SELECT a.item_code , a.rn , a.작업날짜 , REGEXP_SUBSTR(a.to_item_rn, '[^_]+', 1, 1) to_item , REGEXP_SUBSTR(a.to_item_rn, '[^_]+', 1, 2) to_rn , b.아이템변경시간 , a.용량 , LEAD(a.용량) OVER(PARTITION BY a.라인 ORDER BY a.lv) 변경아이템용량 , a.라인 , a.lv FROM w_temp1 a , w_time1 b WHERE a.item_code = b.from_item(+) AND a.rn = b.from_rn(+) AND a.to_item_rn = b.to_item(+)||'_'||b.to_rn(+) ORDER BY a.item_path ;