IT/Oracle

오라클 계층형 쿼리와 ROWNUM

노마드오브 2018. 7. 23. 22:35
SELECT * 
FROM ORCL_BRD_RPL 
START WITH BRD_PNO = 0 -- 시작형
CONNECT BY PRIOR BRD_NO = BRD_PNO -- 연결
ORDER SIBLINGS BY BRD_NO DESC; -- 정렬순서





*계층형 쿼리
SELECT * 
FROM ORCL_BRD_RPL 
START WITH BRD_PNO = 0 -- 시작형
CONNECT BY PRIOR BRD_NO = BRD_PNO -- 연결
ORDER SIBLINGS BY BRD_NO DESC; -- 정렬순서

*ROWNUM으로 순번 부여 및 ROWNUM 조건 사용
SELECT * 
FROM (
SELECT ROWNUM AS RNUM, E.* 
FROM (SELECT * 
FROM EMP 
ORDER BY ENAME ASC) E)
WHERE RNUM BETWEEN 3 AND 6;

*계층형쿼리와 ROWNUM 합치기
SELECT * 
FROM (
SELECT ROWNUM AS RNUM, E.* 
FROM (SELECT * 
FROM ORCL_BRD_RPL 
START WITH BRD_PNO = 0 -- 시작형
CONNECT BY PRIOR BRD_NO = BRD_PNO -- 연결
ORDER SIBLINGS BY BRD_NO DESC -- 정렬순서
) E)
WHERE RNUM BETWEEN 3 AND 6;