Table A:id Name 1 a 2 B 3 C 4 D 5 e table B (Id1,id2 is associated with the ID of table A and is a federated primary foreign key): Id1 Id2 1 2 2 3 2 4 3 5 This is a department-level relationship, the front is the superior, the subordinate, I would like to get all the departments of the list, according to the rank relationship to write a complete string, the results are as follows: ID full_name 1 a 2 A/b 3 a/b/c 4 a/b/d 5 a/b/c/d How do I write? Stored procedures or functions can be, thank you very much!
Sql> CREATE TABLE Table_a (ID number (4), name VARCHAR2 (20));
Table created
sql> INSERT INTO table_a values (1, ' A ');
1 row inserted
sql> INSERT INTO table_a values (2, ' B ');
1 row inserted
sql> INSERT into table_a values (3, ' C ');
1 row inserted
sql> INSERT INTO table_a values (4, ' d ');
1 row inserted
sql> INSERT into table_a values (5, ' e ');
1 row inserted
Sql> commit;
Commit Complete
sql> DROP TABLE Table_b;
Table dropped
Sql> CREATE TABLE Table_b (ID1 number (4), ID2 number (4));
Table created
sql> INSERT into Table_b values (0,1);
1 row inserted
sql> INSERT into table_b values (1,2);
1 row inserted
sql> INSERT into table_b values (2,3);
1 row inserted
sql> INSERT into Table_b values (2,4);
1 row inserted
sql> INSERT into Table_b values (3,5);
1 row inserted
Sql> commit;
Commit Complete
Sql> Select Id2, LTrim (Sys_connect_by_path (NAME, '/'), '/') path 2 from 3 (select B.*, A.name 4 from Table_b B, Table_a A 5 WHERE b.id2=a.id 6 connect by prior ID2=ID1 7 start with ID1 = 0 8 ORDER by Id2 9/
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