Oracle sys_connect_by_path function result set connection

Source: Internet
Author: User

I have seen some people switch between them before. I was just amazed at the time, and it was over. I didn't remember it until when I used it. I started to look for it everywhere, no trace can be found.
Today, I accidentally saw the use of connect by and the usage of sys_connect_by_path, which is an alternative surprise for me. sys_connect_by_path (columnname, seperator) can also be spelled out, however, this function is not used to establish a result set connection for us, but to construct a tree path. Therefore, it must be used together with connect.
Heheh, I am arrogant here. Based on my understanding of some oracle functions, let's look at how I made what we wanted for a common table or result set without a tree structure.
Magic is start.
Item, a common table, is a field name. For example, the table name is test_sysconnectbypath. The table name is too long. You are not afraid of it. It is an alias.
The following table data

NAME
------------------
Shenzhen
Wuhan
Shanghai
Beijing
Tianjin
Singapore

Alias
SQL> with temp as (select name form test_sysconnectbypath );
This is the alias method. The following SQL statement can replace this result set with temp. Of course, this () can be your own complex query result set.
At the beginning of the first change, we changed this into a tree structure.
How can we transform the tree structure into a new one? You can add a pid and a new id. If no, add it to them. However, with the id added, how can we fill in their structural data? Here we need another function to show the secret of lag (). lag () is used to retrieve the previous record, which is opposite to lead, if it is a simple fight, isn't the tree structure, and the previous record is the parent node of the next record?
In this case, we use rownum.
Action
Select t. name, no, lag (no) over (order by no) pid from (select temp. *, rownum no from temp) t;
The result is displayed.

NAME NO PID
----------------------------------------

Shenzhen 1
Wuhan 2 1
Shanghai 3 2
Beijing 4 3
Tianjin 5 4
Singapore 6 5

Now it's a tree.
Change the tree
Action
Select * from (select t. name, no, lag (no) over (order by no) pid from (select temp. *, rownum no from temp) t start with pid is null connect by prior no = pid;
Check the results.

The result is displayed.
NAME NO PID
----------------------------------------
Shenzhen 1
Wuhan 2 1
Shanghai 3 2
Beijing 4 3
Tianjin 5 4
Singapore 6 5

The strange result is not changed. Yes, here we just select the tree. If you add an lpad ('', 4 * level ,'*') | name.
The last change is a string.
Select sys_connect_by_path (name. ',') text from (select t. name, no, lag (no) over (order by no) pid from (select temp. *, rownum no from temp) t start with pid is null connect by prior no = pid;
Check the results by yourself.
Text
--------------------------------------------------

Shenzhen, Wuhan, Shanghai, Beijing, Tianjin, and Singapore

......
Haha, although it was made, but as mentioned above, this is just an alternative joy, because this is not the solution I saw before, but through this method, the powerful connect by function has been analyzed for over. This function is just a sneak peek,
I still need to continue looking for work. When can I find you in the dark.

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.