Oracle Tiered queries

Source: Internet
Author: User

1. When a parent-child relationship exists between rows in a table, the parent-child relationship between rows and rows can be queried by using Connect by query

SelectLpad ('-',( Level-1),'-')||Empno asTempno, Ename,mgr, Level, Decode ( Level,1,1) asRoot, Decode (Connect_by_isleaf,1,1) asLeafnode fromEmpstart withEmpno=7566Connect by(Prior empno)=MgrOrder  by  Level;

In addition to the level pseudo-column, Connect_by_isleaf is also a pseudo-column, 1 means No child records, and 0 is the opposite

2. When one of the attributes in a table needs to be stitched together according to a parent-child relationship, you can use Sys_connect_by_path

 withX1 as   (SelectDeptno,ename,row_number () Over(Partition byDeptnoOrder  byENAME) asRn fromemp1)SelectDeptno,sys_connect_by_path (ename,',') asNamec fromX1whereConnect_by_isleaf=1Start withRn=1Connect by(Prior Deptno)=Deptno and(Prior RN)=Rn-1;

3. For tree query results ordering, it is critical to keep the tree structure correct, so we use the Siblings keyword to sort the branches internally only

SelectLpad ('-',( Level-1),'-')||Empno asTempno, Ename,mgr, Level, Decode ( Level,1,1) asRoot, Decode (Connect_by_isleaf,1,1) asLeafnode fromEmp1start withEmpno=7566Connect by(Prior empno)=MgrOrderSiblings byEmpnodesc;

For a non-parent-child relationship field, you need to make a subquery for the filter condition of the field, and then make a tree query based on that subquery.

4. The above several queries are from the root to the child node query, how to recursively query from the child node to the root, as shown below

Select ename,mgr,level from with   empno=7902  by=  empnoorderby level;

Oracle Tiered queries

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.