한번에 제대로 질문을 못 드려서 죄송합니다
마농님이 풀어주신 MS-SQL은 Recursive 쿼리에서 full scalar 어쩌구 오류가 발생해서
오라클 쿼리를 조금 변경해서 사용했는데요...
아래와 같이 조금 복잡한 가상 데이터를 만들어서 돌려보면 값이 제대로 안 나오네요 ㅠㅠ
WITH T(NO, C1, C2, C3) AS
(--원본 테이블
SELECT 1, 'B', 'B', 'A' FROM SYSIBM.SYSDUMMY1 UNION ALL
SELECT 2, 'C', 'B', 'C' FROM SYSIBM.SYSDUMMY1 UNION ALL
SELECT 3, 'B', 'B', 'C' FROM SYSIBM.SYSDUMMY1 UNION ALL
SELECT 4, 'B', 'B', 'B' FROM SYSIBM.SYSDUMMY1 UNION ALL
SELECT 5, 'B', 'B', 'D' FROM SYSIBM.SYSDUMMY1 UNION ALL
SELECT 6, 'D', 'B', 'B' FROM SYSIBM.SYSDUMMY1 UNION ALL
SELECT 7, 'D', 'B', 'D' FROM SYSIBM.SYSDUMMY1 UNION ALL
SELECT 8, 'D', 'D', 'B' FROM SYSIBM.SYSDUMMY1 UNION ALL
SELECT 9, 'E', 'B', 'E' FROM SYSIBM.SYSDUMMY1 UNION ALL
SELECT 10, 'F', 'B', 'F' FROM SYSIBM.SYSDUMMY1 UNION ALL
SELECT 11, 'C', 'G', 'C' FROM SYSIBM.SYSDUMMY1 UNION ALL
SELECT 12, 'D', 'D', 'H' FROM SYSIBM.SYSDUMMY1 UNION ALL
SELECT 13, 'D', 'D', 'D' FROM SYSIBM.SYSDUMMY1 UNION ALL
SELECT 14, 'D', 'D', 'E' FROM SYSIBM.SYSDUMMY1 UNION ALL
SELECT 15, 'D', 'D', 'I' FROM SYSIBM.SYSDUMMY1 UNION ALL
SELECT 16, 'D', 'D', 'F' FROM SYSIBM.SYSDUMMY1 UNION ALL
SELECT 17, 'J', 'H', 'H' FROM SYSIBM.SYSDUMMY1 UNION ALL
SELECT 18, 'J', 'J', 'H' FROM SYSIBM.SYSDUMMY1 UNION ALL
SELECT 19, 'G', 'K', 'G' FROM SYSIBM.SYSDUMMY1 UNION ALL
SELECT 20, 'L', 'J', 'L' FROM SYSIBM.SYSDUMMY1 UNION ALL
SELECT 21, 'K', 'K', 'K' FROM SYSIBM.SYSDUMMY1 UNION ALL
SELECT 22, 'K', 'K', 'M' FROM SYSIBM.SYSDUMMY1 UNION ALL
SELECT 23, 'K', 'M', 'K' FROM SYSIBM.SYSDUMMY1 UNION ALL
SELECT 24, 'J', 'J', 'L' FROM SYSIBM.SYSDUMMY1 UNION ALL
SELECT 25, 'J', 'J', 'J' FROM SYSIBM.SYSDUMMY1 UNION ALL
SELECT 26, 'J', 'J', 'N' FROM SYSIBM.SYSDUMMY1 UNION ALL
SELECT 27, 'J', 'J', 'O' FROM SYSIBM.SYSDUMMY1 UNION ALL
SELECT 28, 'P', 'J', 'P' FROM SYSIBM.SYSDUMMY1 UNION ALL
SELECT 29, 'N', 'J', 'N' FROM SYSIBM.SYSDUMMY1 UNION ALL
SELECT 30, 'Q', 'K', 'Q' FROM SYSIBM.SYSDUMMY1 UNION ALL
SELECT 31, 'O', 'J', 'O' FROM SYSIBM.SYSDUMMY1 UNION ALL
SELECT 32, 'L', 'L', 'L' FROM SYSIBM.SYSDUMMY1 UNION ALL
SELECT 33, 'R', 'P', 'R' FROM SYSIBM.SYSDUMMY1 UNION ALL
SELECT 34, 'R', 'P', 'P' FROM SYSIBM.SYSDUMMY1 UNION ALL
SELECT 35, 'P', 'R', 'P' FROM SYSIBM.SYSDUMMY1 UNION ALL
SELECT 36, 'N', 'N', 'N' FROM SYSIBM.SYSDUMMY1 UNION ALL
SELECT 37, 'M', 'M', 'M' FROM SYSIBM.SYSDUMMY1 UNION ALL
SELECT 38, 'Q', 'M', 'Q' FROM SYSIBM.SYSDUMMY1 UNION ALL
SELECT 39, 'R', 'R', 'R' FROM SYSIBM.SYSDUMMY1 UNION ALL
SELECT 40, 'R', 'R', 'S' FROM SYSIBM.SYSDUMMY1 UNION ALL
SELECT 41, 'R', 'R', 'T' FROM SYSIBM.SYSDUMMY1 UNION ALL
SELECT 42, 'U', 'R', 'U' FROM SYSIBM.SYSDUMMY1 UNION ALL
SELECT 43, 'T', 'R', 'T' FROM SYSIBM.SYSDUMMY1 UNION ALL
SELECT 44, 'U', V', 'W' FROM SYSIBM.SYSDUMMY1 UNION ALL
SELECT 45, 'X', 'Y', 'Z' FROM SYSIBM.SYSDUMMY1
)
DB2라서 SYSIBM.SYSDUMMY1을 썼습니다.(여기는 오라클 옵션을 막아 놓은 것 같네요..)
참고로 제가 만든 쿼리는 아래와 같습니다.
WITH DATA_TEMP(NO, C1, C2, C3) AS
(SELECT 1, 'B', 'B', 'A' FROM SYSIBM.SYSDUMMY1 UNION ALL
SELECT 2, 'C', 'B', 'C' FROM SYSIBM.SYSDUMMY1 UNION ALL
SELECT 3, 'B', 'B', 'C' FROM SYSIBM.SYSDUMMY1 UNION ALL
SELECT 4, 'B', 'B', 'B' FROM SYSIBM.SYSDUMMY1 UNION ALL
SELECT 5, 'B', 'B', 'D' FROM SYSIBM.SYSDUMMY1 UNION ALL
SELECT 6, 'D', 'B', 'B' FROM SYSIBM.SYSDUMMY1 UNION ALL
SELECT 7, 'D', 'B', 'D' FROM SYSIBM.SYSDUMMY1 UNION ALL
SELECT 8, 'D', 'D', 'B' FROM SYSIBM.SYSDUMMY1 UNION ALL
SELECT 9, 'E', 'B', 'E' FROM SYSIBM.SYSDUMMY1 UNION ALL
SELECT 10, 'F', 'B', 'F' FROM SYSIBM.SYSDUMMY1 UNION ALL
SELECT 11, 'C', 'G', 'C' FROM SYSIBM.SYSDUMMY1 UNION ALL
SELECT 12, 'D', 'D', 'H' FROM SYSIBM.SYSDUMMY1 UNION ALL
SELECT 13, 'D', 'D', 'D' FROM SYSIBM.SYSDUMMY1 UNION ALL
SELECT 14, 'D', 'D', 'E' FROM SYSIBM.SYSDUMMY1 UNION ALL
SELECT 15, 'D', 'D', 'I' FROM SYSIBM.SYSDUMMY1 UNION ALL
SELECT 16, 'D', 'D', 'F' FROM SYSIBM.SYSDUMMY1 UNION ALL
SELECT 17, 'J', 'H', 'H' FROM SYSIBM.SYSDUMMY1 UNION ALL
SELECT 18, 'J', 'J', 'H' FROM SYSIBM.SYSDUMMY1 UNION ALL
SELECT 19, 'G', 'K', 'G' FROM SYSIBM.SYSDUMMY1 UNION ALL
SELECT 20, 'L', 'J', 'L' FROM SYSIBM.SYSDUMMY1 UNION ALL
SELECT 21, 'K', 'K', 'K' FROM SYSIBM.SYSDUMMY1 UNION ALL
SELECT 22, 'K', 'K', 'M' FROM SYSIBM.SYSDUMMY1 UNION ALL
SELECT 23, 'K', 'M', 'K' FROM SYSIBM.SYSDUMMY1 UNION ALL
SELECT 24, 'J', 'J', 'L' FROM SYSIBM.SYSDUMMY1 UNION ALL
SELECT 25, 'J', 'J', 'J' FROM SYSIBM.SYSDUMMY1 UNION ALL
SELECT 26, 'J', 'J', 'N' FROM SYSIBM.SYSDUMMY1 UNION ALL
SELECT 27, 'J', 'J', 'O' FROM SYSIBM.SYSDUMMY1 UNION ALL
SELECT 28, 'P', 'J', 'P' FROM SYSIBM.SYSDUMMY1 UNION ALL
SELECT 29, 'N', 'J', 'N' FROM SYSIBM.SYSDUMMY1 UNION ALL
SELECT 30, 'Q', 'K', 'Q' FROM SYSIBM.SYSDUMMY1 UNION ALL
SELECT 31, 'O', 'J', 'O' FROM SYSIBM.SYSDUMMY1 UNION ALL
SELECT 32, 'L', 'L', 'L' FROM SYSIBM.SYSDUMMY1 UNION ALL
SELECT 33, 'R', 'P', 'R' FROM SYSIBM.SYSDUMMY1 UNION ALL
SELECT 34, 'R', 'P', 'P' FROM SYSIBM.SYSDUMMY1 UNION ALL
SELECT 35, 'P', 'R', 'P' FROM SYSIBM.SYSDUMMY1 UNION ALL
SELECT 36, 'N', 'N', 'N' FROM SYSIBM.SYSDUMMY1 UNION ALL
SELECT 37, 'M', 'M', 'M' FROM SYSIBM.SYSDUMMY1 UNION ALL
SELECT 38, 'Q', 'M', 'Q' FROM SYSIBM.SYSDUMMY1 UNION ALL
SELECT 39, 'R', 'R', 'R' FROM SYSIBM.SYSDUMMY1 UNION ALL
SELECT 40, 'R', 'R', 'S' FROM SYSIBM.SYSDUMMY1 UNION ALL
SELECT 41, 'R', 'R', 'T' FROM SYSIBM.SYSDUMMY1 UNION ALL
SELECT 42, 'U', 'R', 'U' FROM SYSIBM.SYSDUMMY1 UNION ALL
SELECT 43, 'T', 'R', 'T' FROM SYSIBM.SYSDUMMY1
)
, LVL_TEMP(LV) AS
(SELECT 1 AS LV
FROM SYSIBM.SYSDUMMY1
UNION ALL
SELECT LV + 1
FROM LVL_TEMP
WHERE LV < 3
)
, LST_TEMP AS
(SELECT C, PC
FROM (SELECT C, PC
, DENSE_RANK() OVER(PARTITION BY C
ORDER BY CASE
WHEN PC IS NULL THEN '2'
ELSE '1'
END ) AS DR
FROM (SELECT NO
, C
, CASE
WHEN MAX(C) OVER(PARTITION BY NO
ORDER BY C
ROWS BETWEEN 1 PRECEDING AND 1 PRECEDING) = C THEN NULL
ELSE MAX(C) OVER(PARTITION BY NO
ORDER BY C
ROWS BETWEEN 1 PRECEDING AND 1 PRECEDING)
END AS PC
FROM (SELECT NO
, CASE LV
WHEN 1 THEN C1
WHEN 2 THEN C2
WHEN 3 THEN C3
END C
FROM DATA_TEMP F
,(SELECT *
FROM LVL_TEMP
)LV
)LG
)PC
GROUP BY C
, PC
)DR
WHERE DR = 1
)
, RZLT(LV, C, PC, RT) AS
(SELECT 0 AS LV
, CAST(C AS VARCHAR(100)) AS C
, PC
, '' AS RT
FROM LST_TEMP
WHERE PC IS NULL
UNION ALL
SELECT LV + 1, T.C, T.PC, T.C||'|'||R.PC
FROM RZLT R
, LST_TEMP T
WHERE R.C = T.PC
)
SELECT DISTINCT
C
, CASE
WHEN PC IS NULL THEN C
WHEN RT IS NULL THEN PC
ELSE SUBSTR(RT, LOCATE('|', RT) +1)
END
FROM RZLT
WHERE LV <=2
ORDER BY 2, 1
FETCH FIRST 100 ROWS ONLY
WITH UR;