How to Use the SYS_CONNECT_BY_PATH function in Oracle

Source: Internet
Author: User

In Oracle, The SYS_CONNECT_BY_PATH function is a very important function. The following describes an example of using the SYS_CONNECT_BY_PATH function. The example is as follows:

 
 
  1. Create table test (a varchar2 (10), B varchar2 (10 ));
  2.  
  3. Insert into test (A, B) VALUES ('1', 'I ');
  4. Insert into test (A, B) VALUES ('1 ');
  5. Insert into test (A, B) VALUES ('2', '1 ');
  6. Insert into test (A, B) VALUES ('2', 'start ');
  7. COMMIT;
  8.  
  9. Select a, B FROM TEST
  10.  
  11. A B
  12. --------------------
  13. 1. Me
  14. 1.
  15. 2 I
  16. From 2
  17.  
  18. Now we need to achieve the following results,
  19. A B
  20. --------------------
  21. 1. Me
  22. 2, starting from
  23.  

Only one SQL statement is used to return the result.

 
 
  1. SELECT A, LTRIM(MAX(SYS_CONNECT_BY_PATH(B, ',')), ',') B  
  2. FROM (SELECT B, A, ROW_NUMBER() OVER(PARTITION BY A ORDER BY B DESC) RN  
  3.           FROM TEST)  
  4. START WITH RN = 1 
  5. CONNECT BY RN - 1 = PRIOR RN  
  6.        AND A = PRIOR A  
  7. GROUP BY A; 

The SYS_CONNECT_BY_PATH function is used to distinguish all the child nodes under a parent node by a specific character, and then connect them to a column for display.

The row_number function is widely used to generate a sequence number for each row of records queried. The production sequence number method is controlled by the statements in the over () function.
 

Use of Oracle to_char Functions

Implementation of oracle function return table

Learn about the Oracle FBI Index

Multiple table Connection Methods in Oracle

Example of using SQL recursive statements in oracle

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.