Example of using Oracle hierarchical query and with functions

Source: Internet
Author: User

In development, everyone should have done something similar to department management. Generally, there is a lower-level department (sub-department) under the next department. This level is similar to a tree. In this case, the Parent and Child departments are generally divided into two or more tables. This is a common practice; sometimes it is possible to put all departments in a table for the convenience of data table management and add a field to identify the affiliation between departments. This looks a bit strange, but it is OK to use it together. Next I will share with you my experiences, that is, how to create a number chart based on this design. A simple approach is to query a list set, what should I do? First, let's look at my data table.
 
It is not hard to see that fparent is used for association between departments. How to Write SQL statements? The following is a lazy way of writing.
Copy codeThe Code is as follows:
-- | It is a connector. level can be considered as a constant.
Select t. fnum, LPAD ('', 3 * level-3) | '|-' | fname, t. fparent, Level
From m_depart t
-- Start with is followed by condition 1
Start with fparent is null
-- Connect by Prior followed by condition 2
Connect by Prior fnum = fparent

The following is the query result. The query result and number are very close.
 
The following is a commonly used with function, which may be used for complex queries. For more information, see the following 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.