I need help in building a hierarchical query to get parent node based on type. For example:
Table (org)
pid|cid|type|name
| 1|MGT |Ofc Pres
1| 2|DEP |HR
1| 3|DEP |IT
3| 4|DIV |Web
3| 5|DIV |Database
4| 6|SEC |HTML
4| 7|SEC |JAVA
My goal is to get the DEP given the cid (6 or 7). Using the query below, I only get the pid (4).
select pid
from org
start
with cid = 7
connect
by
prior cid = pid
Appreciate any feedback.
UPDATE 07/24/2018:
Some additional info that might help.
MGT (Management) is the highest/root level DEP (Department) is under MGT. DIV (Division) is under DEP. SEC (Section) is under DIV.
So if given a child id of type SEC, I need to get the DEP (Department) which it is under (which means I need to get the DIV first then the DEP). If given a child id of type DIV, then I need to get DEP which it is under.
type = 'DEP'? Do you then need to get thenameof theDEP? What if, going up the tree, you never hitDEP? (Or are you guaranteeing that you always will? Well, obviously that is not possible; if you "start with 1", you will never hitDEP.)