Eu tenho uma tabela que contém uma lista de departamentos de vendas da seguinte forma:
+-------+---------------+-----------+
|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 |
+-------+---------------+-----------+
Eu preciso de uma consulta que retorna todos os subcaminhos possíveis. Assim, para as três primeiras linhas deve ser a seguinte:
5500 5510
5500 5511
5510 5511
por isso para cada possível subpath ele retornaria a primeira e última departamento do caminho. É bastante fácil de obter o caminho, fazendo isso:
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
mas como posso obter todos os subcaminhos possíveis?