sql之22 Hierarchical Retrieval
--語法
SELECT [LEVEL], column, expr...
FROM table
[WHERE condition(s)]
[START WITH condition(s)]
[CONNECT BY PRIOR condition(s)];
WHERE condition: expr comparison_operator expr
--第一種方式:Walking the Tree: From the Bottom Up
SQL> select employee_id,last_name,job_id,manager_id
2 from employees
3 start with employee_id=101
4 connect by prior manager_id=employee_id;
EMPLOYEE_ID LAST_NAME JOB_ID MANAGER_ID
----------- ---------- ---------- ----------
101 Kochhar AD_VP 100
100 King AD_PRES
--第二種方式:Walking the Tree: From the Top Down
SQL> ;
1 select employee_id,last_name,job_id,manager_id
2 from employees
3 start with employee_id=101
4* connect by prior employee_id=manager_id
SQL> /
EMPLOYEE_ID LAST_NAME JOB_ID MANAGER_ID
----------- ---------- ---------- ----------
101 Kochhar AD_VP 100
200 Whalen AD_ASST 101
205 Higgins AC_MGR 101
206 Gietz AC_ACCOUNT 205
-- 理解prior修飾符
SQL> select last_name||' report to '|| prior last_name "Walk Top Down"
2 from employees
3 start with last_name='King'
4 connect by prior employee_id=manager_id;
Walk Top Down
--------------------------------------------------------------
King report to
Kochhar report to King
Whalen report to Kochhar
Higgins report to Kochhar
Gietz report to Higgins
De Haan report to King
Hunold report to De Haan
Ernst report to Hunold
--如何格式化樹狀分級報告?level是個偽列(pseudocolumn)
--Formatting Hierarchical Reports Using LEVEL and LPAD
SQL> column org_chart format a20
SQL> select lpad(last_name,length(last_name)+(level*2)-2,'.')
2 as org_chart
3 from employees
4 start with last_name='King'
5 connect by prior employee_id=manager_id
ORG_CHART
-------------------
King
..Kochhar
....Whalen
....Higgins
......Gietz
-- 如何修剪枝杈?Pruning Branches
SELECT department_id, employee_id,last_name, job_id, salar y
FROM employees
WHERE last_name != 'Higgins'
START WITH manager_id IS NULL
CONNECT BY PRIOR employee_id = manager_id;
-- or
SELECT department_id, employee_id,last_name, job_id, salary
FROM employees
START WITH manager_id IS NULL
CONNECT BY PRIOR employee_id = manager_id
AND last_name != 'Higgins';
-- 測試題:Create a report that shows the hierarchy of the managers for the employee Lorentz. Display his immediate manager first.
SQL> select last_name
2 from employees
3 start with employee_id=(select b.manager_id from employees b where b.last_name='Lorentz')
4 connect by prior manager_id=employee_id
5 and last_name != 'Lorentz'
LAST_NAME
----------
Hunold
De Haan
King
-- test 2: Create an indented report showing the management hierarchy starting from the employee whose LAST_NAME is Kochhar. Print the employee’s last name, manager ID, and department ID. Give alias names to the columns as shown in the sample output.
SQL> col name for a10
SQL> select lpad(last_name,length(last_name)+(level*2)-2,'_') name,
2 manager_id mgr,department_id deptno
3 from employees
4 start with last_name='Kochhar'
5 connect by prior employee_id=manager_id;
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/271063/viewspace-909779/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- [轉]Hierarchical Queries之LEVEL應用
- Information Retrieval(資訊檢索)筆記02:Preprocessing and Tolerant RetrievalORM筆記
- 【層次查詢】Hierarchical Queries之LEVEL應用
- 【層次查詢】Hierarchical Queries之“樹的遍歷”
- 【層次查詢】Hierarchical Queries之處理順序
- 【層次查詢】Hierarchical Queries之CONNECT_BY_ISCYCLE偽列
- 【層次查詢】Hierarchical Queries之CONNECT_BY_ISLEAF偽列
- java.sql.SQLNonTransientConnectionException: Public Key Retrieval is not allowed 報錯的解決方案JavaSQLException
- 【層次查詢】Hierarchical Queries之SYS_CONNECT_BY_PATH函式函式
- yum-GPG key retrieval failed: No such file or directoryAI
- 《CNN Image Retrieval in PyTorch: Training and evaluati-ng CNNs for Image Retrieval in PyTorch》程式碼思路解讀CNNPyTorchAI
- Deep Hashing Network for Efficient Similarity RetrievalMILA
- Agglomerative Hierarchical Clustering詳解
- GPG key retrieval failed: [Errno 14] HTTP Error 404: Not FoundAIHTTPError
- 【層次查詢】Hierarchical Queries之親兄弟間的排序(ORDER SIBLINGS BY)排序
- SQL優化常用方法22SQL優化
- 1223 result cache,sql profile,sql patchSQL
- 【層次查詢】Hierarchical Queries之尋根問祖(CONNECT_BY_ROOT一元運算子)
- SQL Server 之 SQL 注入篇SQLServer
- informix 中層次/樹型/Hierarchical查詢的使用ORM
- SQL Server 2022 AlwaysOn新特性之包含可用性組介紹SQLServer
- sql之27 using sql*plusSQL
- sql之26 using sql*plusSQL
- SQL入門之6 sql*plusSQL
- word2vec原理(二) 基於Hierarchical Softmax的模型模型
- Hierarchical Queries 級聯查詢(樹狀結構查詢)
- DALLE2: Hierarchical Text-Conditional Image Generation with CLIP Latents
- ORACLE之常用FAQ:SQL&PL/SQLOracleSQL
- SQL語法之SQL 萬用字元SQL字元
- Smooth-AP: Smoothing the Path Towards Large-Scale Image Retrieval(翻譯)
- 分層資料 Hierarchical Data 探索 (2.鄰接表模型)模型
- 《Hierarchical Text-Conditional Image Generation with CLIP Latents》閱讀筆記筆記
- SQL Server 2022 RTM 最新累積更新:Cumulative Update #13 for SQL Server 2022 RTMSQLServer
- 【SQL】sql優化小工具之SQLHCSQL優化
- Sql 之 join 001SQL
- sql注入之union注入SQL
- MS SQL之回憶SQL
- 深入SQL之merge intoSQL