Hierarchical Hell - traversing up the tree?
784546Jul 16 2010 — edited Jul 16 2010Still struggling with hierarchical queries here. My assignment was originally to identify "last nodes" whose object_type_id is something other than a 29. You guys helped me do that with the connect_by_isleaf>0 clause, so my SQL is now:
select distinct p.parent_id, lpad(' ',level*2) || p.id, p.object_type_id
from product p
where p.country_id='GBR'
and connect_by_isleaf>0
and p.object_type_id != 29
start with p.id=2173952 -- that's the top of the hierarchy
connect by p.parent_id = prior p.id
order by 1,2;
My next assignment is: For these "last nodes", to traverse back up the tree to see if any of their predecessors have other children (or grand-children, or great-grandchilren etc.) who DO end in an object_type_id = 29.
The result set of these "last nodes" varies as to how deep it is - everything from level 6 thru level 9 (and could conceivably go deeper in the future, so needs to be dynamically scalable).
I could do this the stupid way - define a cursor that identifies the level, and then use that level to keep going backwards. But that's going to be really intensive esp. as data volume grows and I'm wondering if there's a smarter, more efficient way to go about this?
Thanks in advance for your help.