I have a table with rules on an Oracle database:
parent_rule | parent_key |child_rule | child_key
and a second table with the status of the child
rule | key | status
All rules and keys are numbers.
I need a query that looks into every parent of a given child. It should return 1 if all parents of the child have status 1 and 0 if just one parent is having status 0. I found a hierarchical query but I don't know how to traverse the rules with 2 variables instead of one.
I tried a normal hierarchical query with just one variable like
SELECT
child_rule,
CONNECT_BY_ROOT parent_rule, SYS_CONNECT_BY_PATH( parent_rule, '/' )
FROM rules
CONNECT BY PRIOR parent_rule=child_rule;
I don't know how to add the second variable. Also my data shows loops here due to the missing key.
The select I don't have any clue how to do it.
Parent Status
Your question does not contain any sample data or expected output; however, from your description, if you only want to consider parents (and not all ancestors or the child itself) then you do not appear to need a hierarchical query and can use a simple
JOINand then aggregate with conditional aggregation to check if there exists zero rows for each child with a status of0and, if so, output1for that child else output0:Which, for the sample data:
Outputs:
fiddle
Ancestor Status
If you do want ancestors (which the question does not mention) and you want to consider the status of the child and all its ancestors then you can use:
Which, for the sample data:
Outputs:
fiddle