Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Find a branch in organisation tree

user13117585Oct 7 2022

Hello all, 
I have a question. 
I have a hierarchical structure like organisations. In that tree, I have a state attribute for each node.
If a node has a specific state (let say bar), all descending nodes should be the same state(bar).
Is it possible to identify and show the branches that do not match the state where an ancestor has bar?

CREATE TABLE orgs 
(
 id number, 
 pid number, 
 s varchar(10)
);

INSERT INTO orgs(id, pid, s) values(1, null, 'foo');
INSERT INTO orgs(id, pid, s) values(2,  1, 'foo');
INSERT INTO orgs(id, pid, s) values(3,  2, 'foo');
INSERT INTO orgs(id, pid, s) values(4,  3, 'foo');


INSERT INTO orgs(id, pid, s) values(10, null, 'foo');
INSERT INTO orgs(id, pid, s) values(11,  10, 'bar');
INSERT INTO orgs(id, pid, s) values(12,  11, 'foo');
INSERT INTO orgs(id, pid, s) values(13,  12, 'baz');

In first case, all good. 
In second scenario, I'd like to show [11, bar] > [12, foo] > [13 > baz]
Because BAR is the state that when it is set on a node, all descendants should match it.

Is this possible easily?

Regards,

This post has been answered by Frank Kulash on Oct 8 2022
Jump to Answer
Comments
Post Details
Added on Oct 7 2022
3 comments
363 views