【发布时间】:2020-04-30 14:29:57
【问题描述】:
我有一个包含销售部门列表的表格,如下所示:
+-------+---------------+-----------+
|DEPT_ID|DEPT_NAME |DEPT_PARENT|
+-------+---------------+-----------+
|5500 |World | |
|5510 |Region 1 |5500 |
|5511 |Cell 1 Region 1|5510 |
|5512 |Cell 2 Region 1|5510 |
|5513 |Cell 3 Region 1|5510 |
|5514 |Cell 4 Region 1|5510 |
|5515 |Cell 5 Region 1|5510 |
|5520 |Region 2 |5500 |
|5521 |Cell 1 Region 2|5520 |
|5522 |Cell 2 Region 2|5520 |
|5530 |Region 3 |5500 |
|5531 |Cell 1 Region 3|5530 |
|5532 |Cell 2 Region 3|5530 |
|5540 |Region 4 |5500 |
|5533 |Cell 1 Region 4|5540 |
|5534 |Cell 2 Region 4|5533 |
|5590 |Region 5 |5500 |
|5591 |Cell 1 Region 5|5590 |
+-------+---------------+-----------+
我需要一个返回所有可能子路径的查询。所以前三行应该如下:
5500 5510
5500 5511
5510 5511
因此对于每个可能的子路径,它将返回路径的第一个和最后一个部门。这样做很容易获得路径:
SELECT d.*, LTRIM (SYS_CONNECT_BY_PATH (dept_id, '-'), '-') AS PATH
FROM depts d
START WITH dept_parent IS NULL
CONNECT BY PRIOR dept_id = dept_parent
但是我怎样才能获得所有可能的子路径?
【问题讨论】:
标签: sql oracle hierarchy hierarchical-data