아래 test data 결과처럼 나오고 싶습니다.
RPEV_APP_ID 이 NULL인걸로 시작해서 APP_ID의 마지막 노드까지 가서 마지막 노드의 값을 가져오고 싶습니다.
START WITH 를 RPEV_APP_ID IS NULL로 시작하니 ROOT의 값은 나오는데...
마지막 노의 값을 나오게 할려면 어떻게 해야하는지 궁금해서 문의 드립니다.
================================================================================================
WITH TEST_DATA AS
(
SELECT '1' AS TYPE_ID
,'TYPE1 결재' AS TYPE_APP_NM
,155 AS APP_ID --결재ID
,154 AS RPEV_APP_ID --이전결재ID
,'반려' AS APP_ID_NM
FROM DUAL
UNION ALL
SELECT '1' AS TYPE_ID
,'TYPE1 결재' AS TYPE_APP_NM
,154 AS APP_ID --결재ID
,122 AS RPEV_APP_ID --이전결재ID
,'반려' AS APP_ID_NM
FROM DUAL
UNION ALL
SELECT '1' AS TYPE_ID
,'TYPE1 결재' AS TYPE_APP_NM
,122 AS APP_ID --결재ID
,120 AS RPEV_APP_ID --이전결재ID
,'반려' AS APP_ID_NM
FROM DUAL
UNION ALL
SELECT '1' AS TYPE_ID
,'TYPE1 결재' AS TYPE_APP_NM
,120 AS APP_ID --결재ID
,NULL AS RPEV_APP_ID --이전결재ID
,'반려' AS APP_ID_NM
FROM DUAL
UNION ALL
SELECT '1' AS TYPE_ID
,'TYPE1 결재' AS TYPE_APP_NM
,100 AS APP_ID --결재ID
,99 AS RPEV_APP_ID --이전결재ID
,'결재' AS APP_ID_NM
FROM DUAL
UNION ALL
SELECT '1' AS TYPE_ID
,'TYPE1 결재' AS TYPE_APP_NM
,99 AS APP_ID --결재ID
,88 AS RPEV_APP_ID --이전결재ID
,'반려' AS APP_ID_NM
FROM DUAL
UNION ALL
SELECT '1' AS TYPE_ID
,'TYPE1 결재' AS TYPE_APP_NM
,88 AS APP_ID --결재ID
,87 AS RPEV_APP_ID --이전결재ID
,'반려' AS APP_ID_NM
FROM DUAL
UNION ALL
SELECT '1' AS TYPE_ID
,'TYPE1 결재' AS TYPE_APP_NM
,87 AS APP_ID --결재ID
,NULL AS RPEV_APP_ID --이전결재ID
,'반려' AS APP_ID_NM
FROM DUAL
UNION ALL
SELECT '2' AS TYPE_ID
,'TYPE2 결재' AS TYPE_APP_NM
,86 AS APP_ID --결재ID
,85 AS RPEV_APP_ID --이전결재ID
,'반려' AS APP_ID_NM
FROM DUAL
UNION ALL
SELECT '2' AS TYPE_ID
,'TYPE2 결재' AS TYPE_APP_NM
,85 AS APP_ID --결재ID
,84 AS RPEV_APP_ID --이전결재ID
,'반려' AS APP_ID_NM
FROM DUAL
UNION ALL
SELECT '2' AS TYPE_ID
,'TYPE2 결재' AS TYPE_APP_NM
,84 AS APP_ID --결재ID
,NULL AS RPEV_APP_ID --이전결재ID
,'반려' AS APP_ID_NM
FROM DUAL
)
SELECT TYPE_ID
,TYPE_APP_NM
,APP_ID
,RPEV_APP_ID
,APP_ID_NM
--,CONNECT_BY_ISLEAF AS LEAF_NODE
FROM TEST_DATA
WHERE 1=1
-- START WITH RPEV_APP_ID IS NULL
-- CONNECT BY APP_ID = RPEV_APP_ID
-- 결과
--1 TYPE1 결재 155 154 반려
--1 TYPE1 결재 100 99 결재
--2 TYPE2 결재 86 85 반려 |