[퀴즈] 계층구조쿼리의 응용 1 6 6,101

by 마농 sys_connect_by_path reverse [2010.01.11 14:18:42]


[퀴즈] 계층구조쿼리의 응용

emp 테이블을 이용하여 다음과 같은 결과를 도출하세요.
- ename_asc : empno와 mgr의 계층관계를 ename을 연결시켜 표현
- ename_desc : ename_asc 를 역으로 표현

WITH emp AS
(
SELECT 7369 empno, 'SMITH' ename, 7902 mgr FROM dual
UNION ALL SELECT 7499, 'ALLEN' , 7698 FROM dual
UNION ALL SELECT 7521, 'WARD'  , 7698 FROM dual
UNION ALL SELECT 7566, 'JONES' , 7839 FROM dual
UNION ALL SELECT 7654, 'MARTIN', 7698 FROM dual
UNION ALL SELECT 7698, 'BLAKE' , 7839 FROM dual
UNION ALL SELECT 7782, 'CLARK' , 7839 FROM dual
UNION ALL SELECT 7788, 'SCOTT' , 7566 FROM dual
UNION ALL SELECT 7839, 'KING'  , NULL FROM dual
UNION ALL SELECT 7844, 'TURNER', 7698 FROM dual
UNION ALL SELECT 7876, 'ADAMS' , 7788 FROM dual
UNION ALL SELECT 7900, 'JAMES' , 7698 FROM dual
UNION ALL SELECT 7902, 'FORD'  , 7566 FROM dual
UNION ALL SELECT 7934, 'MILLER', 7782 FROM dual
)
SELECT * FROM emp;

[결과]

EMPNO

ENAME

MGR

LV

ENAME_ASC

ENAME_DESC

7839 KING   1 KING KING
7566 JONES 7839 2 KING-JONES JONES-KING
7788 SCOTT 7566 3 KING-JONES-SCOTT SCOTT-JONES-KING
7876 ADAMS 7788 4 KING-JONES-SCOTT-ADAMS ADAMS-SCOTT-JONES-KING
7902 FORD 7566 3 KING-JONES-FORD FORD-JONES-KING
7369 SMITH 7902 4 KING-JONES-FORD-SMITH SMITH-FORD-JONES-KING
7698 BLAKE 7839 2 KING-BLAKE BLAKE-KING
7499 ALLEN 7698 3 KING-BLAKE-ALLEN ALLEN-BLAKE-KING
7521 WARD 7698 3 KING-BLAKE-WARD WARD-BLAKE-KING
7654 MARTIN 7698 3 KING-BLAKE-MARTIN MARTIN-BLAKE-KING
7844 TURNER 7698 3 KING-BLAKE-TURNER TURNER-BLAKE-KING
7900 JAMES 7698 3 KING-BLAKE-JAMES JAMES-BLAKE-KING
7782 CLARK 7839 2 KING-CLARK CLARK-KING
7934 MILLER 7782 3 KING-CLARK-MILLER MILLER-CLARK-KING

정답은 몇일 뒤에 올리도록 하겠습니다.

[참조] http://www.soqool.com/servlet/board?cmd=view&cat=100&subcat=1050&seq=2385&page=1&position=1

[정답보기] <=== 트리플클릭
SELECT empno
     , ename
     , mgr
     , LEVEL lv
     , SUBSTR(SYS_CONNECT_BY_PATH(ename,'-'),2) ename_asc
     , REVERSE(SUBSTR(SYS_CONNECT_BY_PATH(REVERSE(ename),'-'),2)) ename_desc
  FROM emp
 START WITH mgr IS NULL
 CONNECT BY PRIOR empno = mgr
;

by 현 [2010.01.12 10:03:28]
다 구했는데 ENAME_DESC 여기서 딱 걸리네....

by 현 [2010.01.12 10:15:04]
완전 허접하네요....

WITH emp AS
(
SELECT 7369 empno, 'SMITH' ename, 7902 mgr FROM dual
UNION ALL SELECT 7499, 'ALLEN' , 7698 FROM dual
UNION ALL SELECT 7521, 'WARD' , 7698 FROM dual
UNION ALL SELECT 7566, 'JONES' , 7839 FROM dual
UNION ALL SELECT 7654, 'MARTIN', 7698 FROM dual
UNION ALL SELECT 7698, 'BLAKE' , 7839 FROM dual
UNION ALL SELECT 7782, 'CLARK' , 7839 FROM dual
UNION ALL SELECT 7788, 'SCOTT' , 7566 FROM dual
UNION ALL SELECT 7839, 'KING' , NULL FROM dual
UNION ALL SELECT 7844, 'TURNER', 7698 FROM dual
UNION ALL SELECT 7876, 'ADAMS' , 7788 FROM dual
UNION ALL SELECT 7900, 'JAMES' , 7698 FROM dual
UNION ALL SELECT 7902, 'FORD' , 7566 FROM dual
UNION ALL SELECT 7934, 'MILLER', 7782 FROM dual
)
SELECT empno, ename, mgr, level lv,
SUBSTR(SYS_CONNECT_BY_PATH(ename, '-'),2) ENAME_ASC
,(SELECT MAX(SUBSTR(SYS_CONNECT_BY_PATH(ename, '-'),2))
FROM emp B
START WITH A.empno = B.empno
CONNECT BY PRIOR MGR = empno ) ENAME_DESC
FROM emp A
START WITH ename='KING'
CONNECT BY PRIOR empno= MGR

잘 모르면 이렇게 노가다를...ㅋㅋㅋ

어쨌든 결과는 나오므로 패스~~!

by 동일1457 [2010.01.12 11:17:42]
심심했는데 문제 올려주셨군요 마농님 감사^^

WITH EMP AS
(
SELECT 7369 EMPNO, 'SMITH' ENAME, 7902 MGR FROM DUAL
UNION ALL SELECT 7499, 'ALLEN' , 7698 FROM DUAL
UNION ALL SELECT 7521, 'WARD' , 7698 FROM DUAL
UNION ALL SELECT 7566, 'JONES' , 7839 FROM DUAL
UNION ALL SELECT 7654, 'MARTIN', 7698 FROM DUAL
UNION ALL SELECT 7698, 'BLAKE' , 7839 FROM DUAL
UNION ALL SELECT 7782, 'CLARK' , 7839 FROM DUAL
UNION ALL SELECT 7788, 'SCOTT' , 7566 FROM DUAL
UNION ALL SELECT 7839, 'KING' , NULL FROM DUAL
UNION ALL SELECT 7844, 'TURNER', 7698 FROM DUAL
UNION ALL SELECT 7876, 'ADAMS' , 7788 FROM DUAL
UNION ALL SELECT 7900, 'JAMES' , 7698 FROM DUAL
UNION ALL SELECT 7902, 'FORD' , 7566 FROM DUAL
UNION ALL SELECT 7934, 'MILLER', 7782 FROM DUAL
)
SELECT EMPNO
, ENAME
, MGR
, LEVEL LV
, RTRIM(REVERSE(SYS_CONNECT_BY_PATH(REVERSE(ENAME),'-')),'-') ENAME_ASC
FROM EMP
START WITH MGR IS NULL
CONNECT BY PRIOR EMPNO = MGR

by 현 [2010.01.12 11:31:26]
아하....REVERSE가 있군요...
하나 배웠네요...

by 서성우 [2010.01.12 11:45:23]
아.....

reverse함수가 있었군요?

허..... 저도 하나 배웠네요

감사합니다.

by Ejql [2010.01.14 00:00:00]
reverse는 공식적으로 나온 함수(내부적으로만 알려져 있다고...)가 아니라고 알고있습니다. 사용하다 튀면 책임 못진다라고 하는데요. 사용해서 문제가 되었던 적이 있는지가 걱정이 됩니다.
댓글등록
SQL문을 포맷에 맞게(깔끔하게) 등록하려면 code() 버튼을 클릭하여 작성 하시면 됩니다.
로그인 사용자만 댓글을 작성 할 수 있습니다. 로그인, 회원가입