예를 들면 아래 처럼 데이터가 들어 있다면 각 레벨 1에 하위에 있는 2,3레벨의 로우수를 카운트하여
구할 수 있는지 궁금합니다. 아니면 이걸 그냥 자바에서 처리해야 하는건지..
원하는 출력 값은
SELECT
KEY, UPPER_KEY, TEXT, 하위레벨의 로우수
FROM
CONNECT BY PRIOR CLASS_CODE = UPPER_CLASS_CODE
START WITH CLASS_CODE = 'ROOT'
LEVEL 1 KEY 1000001 UPPER_KEY ROOT TEXT 1
2 2000001 1000001 1
3 3000001 2000001 1
1 1000002 ROOT 1
2 2000002 1000002 1
3 3000002 2000001 1
WITH t AS ( SELECT '1000004' key_code, '1000004' class_code, 'ROOT' upper_class_code FROM dual UNION ALL SELECT '1000004', '2000005', '1000004' FROM dual UNION ALL SELECT '1000004', '3000015', '2000005' FROM dual UNION ALL SELECT '1000005', '1000005', 'ROOT' FROM dual UNION ALL SELECT '1000005', '2000006', '1000005' FROM dual UNION ALL SELECT '1000006', '1000006', 'ROOT' FROM dual UNION ALL SELECT '1000006', '2000007', '1000006' FROM dual ) SELECT a.lv , a.key_code , a.class_code , a.upper_class_code , b.cnt FROM (SELECT LEVEL lv , key_code , class_code , upper_class_code , ROWNUM rn FROM t START WITH upper_class_code = 'ROOT' CONNECT BY PRIOR key_code = key_code AND PRIOR class_code = upper_class_code ) a , (SELECT key_code, class_code , COUNT(*) - 1 cnt FROM (SELECT CONNECT_BY_ROOT key_code key_code , CONNECT_BY_ROOT class_code class_code FROM t CONNECT BY PRIOR key_code = key_code AND PRIOR class_code = upper_class_code ) GROUP BY key_code, class_code ) b WHERE a.key_code = b.key_code AND a.class_code = b.class_code ORDER BY rn ;