select AD_DEPARTMENT_CODE, AD_DEPARTMENT_UP_LEVEL, AD_DEPARTMENT_NAME from (select AD_DEPARTMENT_CODE, AD_DEPARTMENT_UP_LEVEL, AD_DEPARTMENT_NAME from AD_DEPT_ARCHITECTURE order by AD_DEPARTMENT_UP_LEVEL, AD_DEPARTMENT_CODE) start with AD_DEPARTMENT_CODE = '0' connect by prior AD_DEPARTMENT_CODE = AD_DEPARTMENT_UP_LEVEL;
|
|
SELECT APR_CODE, APR_CODE_UP, NAMES FROM (SELECT A.MEDIA_CODE || '|' || A.CHANNEL_CODE || '|' || A.COLUMN_CODE AS APR_CODE, A.MEDIA_CODE_UP || '|' || A.CHANNEL_CODE_UP || '|' || A.COLUMN_CODE_UP AS APR_CODE_UP, DECODE((B.MEDIA_NAME || '-' || C.CHANNEL_CODE || '-' || D.COLUMN_NAME), '--', '产品关系', (B.MEDIA_NAME || '|' || C.CHANNEL_CODE || '|' || D.COLUMN_NAME)) NAMES FROM COLUMNS D, CHANNEL C, MEDIA B, AD_PRODUCT_RELATION A WHERE A.MEDIA_CODE = B.MEDIA_CODE(+) AND A.MEDIA_CODE = C.MEDIA_CODE(+) AND A.CHANNEL_CODE = C.CHANNEL_CODE(+) AND A.MEDIA_CODE = D.MEDIA_CODE(+) AND A.CHANNEL_CODE = D.CHANNEL_CODE(+) AND A.COLUMN_CODE = D.COLUMN_CODE(+)) START WITH APR_CODE = '0|0|0' CONNECT BY PRIOR APR_CODE = APR_CODE_UP
这里利用'-' 和'|'来进行区分,找出了树状结构的根 为了构建树, (1)先建立了树结构的根,字段这里都设为“0”或者“00” (2)然后再根据需要填充数据 (3)利用上边的oracle的 PRIOR,CONNECT_BY_ROOT 等进行层次查询
|
|
|
|
错误原因:在数据库表TRASFER中,TRASFER字段为number类型,而TAPE_NO字段为Varchar2类型。由于在进行父子关系判定中“TRANSFER = TAPE_NO”两者的字段类型不同而导致:ORA-01436
正确语句: SELECT TRANSFER, TAPE_NO, TRANSFER_NAME FROM (SELECT TRANSFER, TAPE_NO, TRANSFER_NAME FROM TRANSFER ORDER BY TAPE_NO, TRANSFER) START WITH TRANSFER = 0 CONNECT BY PRIOR TO_CHAR(TRANSFER) = TAPE_NO |
|
|
参考一
PL/SQL基础:阶层查询 作者:itpub cxck 2007-05-08 内容导航: ORACLE 10g 新增了阶层查询操作符PRIOR,CONNECT_BY_ROOT SQL>SELECT last_name, employee_id, manager_id, LEVEL FROM employees START WITH employee_id = 100 CONNECT BY PRIOR employee_id = manager_id ORDER SIBLINGS BY last_name; LAST_NAME EMPLOYEE_ID MANAGER_ID LEVEL ------------------------- ----------- ---------- ---------- King 100 1 Cambrault 148 100 2 Bates 172 148 3 Bloom 169 148 3 Fox 170 148 3 Kumar 173 148 3 Ozer 168 148 3 Smith 171 148 3 De Haan 102 100 2 Hunold 103 102 3 Austin 105 103 4 Ernst 104 103 4 Lorentz 107 103 4 Pataballa 106 103 4 Errazuriz 147 100 2 Ande 166 147 3 Banda 167 147 3
SQL>UPDATE employees SET manager_id = 145 WHERE employee_id = 100; SQL>SELECT last_name "Employee", LEVEL, SYS_CONNECT_BY_PATH(last_name, '/') "Path" FROM employees WHERE level <= 3 AND department_id = 80 START WITH last_name = 'King' CONNECT BY PRIOR employee_id = manager_id AND LEVEL <= 4; 2 3 4 5 6 7 ERROR: ORA-01436: CONNECT BY loop in user data CONNECT BY NOCYCLE强制返回查询结果。CONNECT_BY_ISCYCLE显示是否存在LOOP。 SQL>SELECT last_name "Employee", CONNECT_BY_ISCYCLE "Cycle", LEVEL, SYS_CONNECT_BY_PATH(last_name, '/') "Path" FROM employees WHERE level <= 3 AND department_id = 80 START WITH last_name = 'King' CONNECT BY NOCYCLE PRIOR employee_id = manager_id AND LEVEL <= 4; Employee Cycle LEVEL Path ------------------------- ------ ------ ------------------------- Russell 1 2 /King/Russell Tucker 0 3 /King/Russell/Tucker Bernstein 0 3 /King/Russell/Bernstein Hall 0 3 /King/Russell/Hall Olsen 0 3 /King/Russell/Olsen Cambrault 0 3 /King/Russell/Cambrault Tuvault 0 3 /King/Russell/Tuvault Partners 0 2 /King/Partners King 0 3 /King/Partners/King Sully 0 3 /King/Partners/Sully McEwen 0 3 /King/Partners/McEwen
SELECT last_name "Employee", CONNECT_BY_ROOT last_name "Manager", LEVEL-1 "Pathlen", SYS_CONNECT_BY_PATH(last_name, '/') "Path" FROM employees WHERE LEVEL > 1 and department_id = 110 CONNECT BY PRIOR employee_id = manager_id; Employee Manager Pathlen Path --------------- ------------ ---------- ----------------------------------- Higgins Kochhar 1 /Kochhar/Higgins Gietz Kochhar 2 /Kochhar/Higgins/Gietz Gietz Higgins 1 /Higgins/Gietz Higgins King 2 /King/Kochhar/Higgins Gietz King 3 /King/Kochhar/Higgins/Gietz
SELECT name, SUM(salary) "Total_Salary" FROM ( SELECT CONNECT_BY_ROOT last_name as name, Salary FROM employees WHERE department_id = 110 CONNECT BY PRIOR employee_id = manager_id) GROUP BY name; NAME Total_Salary ------------------------- ------------ Gietz 8300 Higgins 20300 King 20300 Kochhar 20300
1
|
参考二
|
||||||||||
|
||||||||||
|
||||||||||
对于oracle进行简单树查询(递归查询)
|