1 / 14

Oracle CONNECT BY function

Oracle CONNECT BY function. JAVA WEB Programming. Emp 테이블의 내용 ( 상 / 하급자 계층구조 ). SQL> select * from emp; EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO ---------- ---------- --------- ---------- -------- ---------- ---------- -

asher
Download Presentation

Oracle CONNECT BY function

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Oracle CONNECT BY function JAVA WEB Programming

  2. Emp 테이블의 내용(상/하급자 계층구조) SQL> select * from emp; EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO ---------- ---------- --------- ---------- -------- ---------- ---------- - 7369 SMITH CLERK 7902 80/12/17 800 20 7499 ALLEN SALESMAN 7698 81/02/20 1600 300 30 7521 WARD SALESMAN 7698 81/02/22 1250 500 30 7566 JONES MANAGER 7839 81/04/02 2975 20 7654 MARTIN SALESMAN 7698 81/09/28 1250 1400 30 7698 BLAKE MANAGER 7839 81/05/01 2850 30 7782 CLARK MANAGER 7839 81/06/09 2450 10 7788 SCOTT ANALYST 7566 87/04/19 3000 20 7839 KING PRESIDENT 81/11/17 5000 10 7844 TURNER SALESMAN 7698 81/09/08 1500 0 30 7876 ADAMS CLERK 7788 87/05/23 1100 20 7900 JAMES CLERK 7698 81/12/03 950 30 7902 FORD ANALYST 7566 81/12/03 3000 20 7934 MILLER CLERK 7782 82/01/23 1300 10 14 개의 행이 선택되었습니다.

  3. Oracle CONNECT BY function SQL> select empno, ename, mgr from emp START WITH ename='KING' CONNECT BY PRIOR empno=mgr; EMPNO ENAME MGR ---------- ---------- ---------- 7839 KING 7566 JONES 7839 7788 SCOTT 7566 7876 ADAMS 7788 7902 FORD 7566 7369 SMITH 7902 7698 BLAKE 7839 7499 ALLEN 7698 7521 WARD 7698 7654 MARTIN 7698 7844 TURNER 7698 7900 JAMES 7698 7782 CLARK 7839 7934 MILLER 7782 14 개의 행이 선택되었습니다.

  4. LEVEL SQL> select LEVEL, empno, ename, mgr from emp START WITH ename='KING' CONNECT BYPRIOR empno=mgr; LEVEL EMPNO ENAME MGR ---------- ---------- ---------- ---------- 1 7839 KING 2 7566 JONES 7839 3 7788 SCOTT 7566 4 7876 ADAMS 7788 3 7902 FORD 7566 4 7369 SMITH 7902 2 7698 BLAKE 7839 3 7499 ALLEN 7698 3 7521 WARD 7698 3 7654 MARTIN 7698 3 7844 TURNER 7698 3 7900 JAMES 7698 2 7782 CLARK 7839 3 7934 MILLER 7782 14 개의 행이 선택되었습니다.

  5. 특정인의 모든 상급자 출력 SQL> select LEVEL, empno, ename, mgr from emp START WITH ename='MILLER' CONNECT BY PRIOR mgr=empno; LEVEL EMPNO ENAME MGR ---------- ---------- ---------- ---- 1 7934 MILLER 7782 2 7782 CLARK 7839 3 7839 KING

  6. LPAD() function LPAD(S,N,X): Returns the string S concatenate to the left by characters X to the total size of N.Example: LPAD(‘MyChar’, 10, ‘O’) returns ‘OOOOMyChar’ SQL> select LPAD(' ', (LEVEL-1)*3, '-') || ename from emp START WITH ename='KING' CONNECT BY PRIOR empno=mgr; LPAD('',(LEVEL-1)*3,'-')||ENAME ------------------------------------------------------------------------ KING -- JONES ----- SCOTT -------- ADAMS ----- FORD -------- SMITH -- BLAKE ----- ALLEN ----- WARD ----- MARTIN ----- TURNER ----- JAMES -- CLARK ----- MILLER 14 개의 행이 선택되었습니다.

  7. LPAD() function SQL> select LPAD('|--', (LEVEL-1)*5, ' ')||ename as 조직도from emp START WITH ename='KING' CONNECT BY PRIOR empno=mgr; 조직도 ------------------------------------------------------------------------ KING |--JONES |--SCOTT |--ADAMS |--FORD |--SMITH |--BLAKE |--ALLEN |--WARD |--MARTIN |--TURNER |--JAMES |--CLARK |--MILLER 14 개의 행이 선택되었습니다.

  8. Hierarchy & Paging SQL> select 조직도 from (select ROWNUM rn, LPAD('|--', (LEVEL-1)*5, ' ')||ename as 조직도 from emp START WITH ename='KING' CONNECT BY PRIOR empno=mgr) where rn between 1 and 5; 조직도 ------------------------------------------ KING |--JONES |--SCOTT |--ADAMS |--FORD

  9. Hierarchy & Paging SQL> select 조직도 from (select ROWNUM rn, LPAD('|--', (LEVEL-1)*5, ' ')||ename as 조직도 from emp START WITH ename='KING' CONNECT BY PRIOR empno=mgr) where rn between 6 and 10; 조직도 -------------------------------------------------- |--SMITH |--BLAKE |--ALLEN |--WARD |--MARTIN

  10. Hierarchy & Paging SQL> select 조직도 from (select ROWNUM rn, LPAD('|--', (LEVEL-1)*5, ' ')||ename as 조직도 from emp START WITH ename='KING' CONNECT BY PRIOR empno=mgr) where rn between 11 and 15; 조직도 -------------------------------------------------------- |--TURNER |--JAMES |--CLARK |--MILLER

  11. ORDER SIBLINGS BY Oracle’s CONNECT BY syntax implies an ordering in which each parent node is followed by its immediate children, with each child followed by its own immediate children, and so on. It’s rare to write a standard ORDER BY clause into a CONNECT BY query because the resulting sort destroys the hierarchical ordering of the data. However, beginning in Oracle9i you can use the new ORDER SIBLINGS BY clause to sort each level independently without destroying the hierarchy: select empno, ename, mgr from emp START WITH ename='KING' CONNECT BY PRIOR empno=mgr; select empno, ename, mgr from emp START WITH ename='KING' CONNECT BY PRIOR empno=mgr order siblings by empno desc

  12. ORDER BY with CONNECT BY SQL> select * from emp start with ename='KING' connect by prior empno=mgr order by empno desc; EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO ---------- ---------- --------- ---------- -------- ---------- ---------- ---------- 7934 MILLER CLERK 7782 82/01/23 1300 10 7902 FORD ANALYST 7566 81/12/03 3000 20 7900 JAMES CLERK 7698 81/12/03 950 30 7876 ADAMS CLERK 7788 87/05/23 1100 20 7844 TURNER SALESMAN 7698 81/09/08 1500 0 30 7839 KING PRESIDENT 81/11/17 5000 10 7788 SCOTT ANALYST 7566 87/04/19 3000 20 7782 CLARK MANAGER 7839 81/06/09 2450 10 7698 BLAKE MANAGER 7839 81/05/01 2850 30 7654 MARTIN SALESMAN 7698 81/09/28 1250 1400 30 7566 JONES MANAGER 7839 81/04/02 2975 20 EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO ---------- ---------- --------- ---------- -------- ---------- ---------- ---------- 7521 WARD SALESMAN 7698 81/02/22 1250 500 30 7499 ALLEN SALESMAN 7698 81/02/20 1600 300 30 7369 SMITH CLERK 7902 80/12/17 800 20 14 개의 행이 선택되었습니다.

  13. ORDER SIBLINGS BY 적용하기 전 SQL> select empno, ename, mgr from emp START WITH ename='KING' CONNECT BY PRIOR empno=mgr; EMPNO ENAME MGR ---------- ---------- ---------- 7839 KING 7566 JONES 7839 7788 SCOTT 7566 7876 ADAMS 7788 7902 FORD 7566 7369 SMITH 7902 7698 BLAKE 7839 7499 ALLEN 7698 7521 WARD 7698 7654 MARTIN 7698 7844 TURNER 7698 EMPNO ENAME MGR ---------- ---------- ---------- 7900 JAMES 7698 7782 CLARK 7839 7934 MILLER 7782 14 개의 행이 선택되었습니다.

  14. ORDER SIBLINGS BY 적용한 후 SQL> select empno, ename, mgr from emp START WITH ename='KING' CONNECT BY PRIOR empno=mgr order siblings by empno desc; EMPNO ENAME MGR ---------- ---------- ---------- 7839 KING 7782 CLARK 7839 7934 MILLER 7782 7698 BLAKE 7839 7900 JAMES 7698 7844 TURNER 7698 7654 MARTIN 7698 7521 WARD 7698 7499 ALLEN 7698 7566 JONES 7839 7902 FORD 7566 EMPNO ENAME MGR ---------- ---------- ---------- 7369 SMITH 7902 7788 SCOTT 7566 7876 ADAMS 7788 14 개의 행이 선택되었습니다.

More Related