Skip to content
Advertisement

Oracle Hierarchy – How to get a particular level record in a query?

We have the following structure for Organizations.

Query returns the result

enter image description here

What I’m trying to do is let’s say I need to get the Department (Information Technology Department) of org_id 4 (Database Unit), how should I build the query for that?

Currently we built a function that returns the department id which basically loops until it reaches the DEP parent. But it has a performance issue.

Advertisement

Answer

You can invert the hierarchy query to start from the Database Unit (org_id = 4) and filter to the department ancestor (org_type = 'DEP'):

The where clause is applied after the hierarchy is retrieved.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement