Oracle Recursive query child nodes

Source: Internet
Author: User

The child node is traced to the root node.

SELECT * from persons.dept start with deptid=76 connect by prior Paredeptid=deptid

Traverse the child nodes (without the root node) through the root node.

SELECT * from persons.dept start with Paredeptid=0 connect by prior Deptid=paredeptid

Traverse a child node (containing the root node) through the root node.

SELECT * from persons.dept start with Deptid=0 connect by prior Deptid=paredeptid

The level keyword allows you to query your hierarchy.

Select A.*,level from Persons.dept a start with Paredeptid=0 connect by prior Deptid=paredeptid

Note:
Start with the use of Connect by, the start with behind is the seed of recursion.
The seed of recursion is the place where the recursion begins. The "prior" in the back of connect by is the default: it can only query the starting line that matches the condition, and does not make a recursive query;
The fields that are placed behind connect by prior are related and indicate the direction of the query.

Exercise: Get the top node from a child node
Select First_value (DeptID) over (ORDER by level DESC ROWS unbounded preceding) as Firstdeptid from persons.dept start with Deptid=76 Connect by Prior Paredeptid=deptid
Table structure
DeptID Paredeptid NAME
Number number CHAR (+ Byte)
Department ID parent Department ID (owning Department ID) Department name

Oracle Recursive query child nodes

Related Article

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.