I have 2 tables one of which defines the hierarchy for the other
equipment
id desc
1 item1
2 item2
3 item3
4 item4
5 item5
6 item6
7 item7
8 item8
9 item9
hierarchy
equipId parentEquipId type attrib_1 attrib_2 attrib3
1 3 D blue red green
2 4 B green blue red
3 2 C orange green purple
4 null A pink yellow orange
5 3 D yellow red purple
6 3 D purple green red
7 2 C grey brown tan
8 7 D red grey yellow
9 7 D blue green orange
My query is against the items of type D. These can be easily determined through a join on the second table to obtain the type.
select *
from equipment
left join on hierarchy.equipId = equipment.id
where hierarchy.type = 'D'
The tricky part is I also need to obtain the 3 attributes for the various parent types. Each type D item has an A,B,C parent hierarchy based on the types of the parents. So for the piece of equipment with the id of 1 the ids of the parents are 3(C),2(B),4(A).
I've been using multiple CONNECT BY PRIOR statements to get the attributes of the parent hierarchy. I have only figured out how to get each attribute individually though and this adds up to a lot of connect by prior statements. Even for each of the parent types I still need to do 3 connect by priors, one for each attribute. It would be nice to be able to do a connect by prior once to each of the parent types and pull all 3 attributes at the same time.
Does anyone know if/how it can be done?
Here's my current SQL. It works but I'd like to make it more efficient and less verbose.
Any help is appreciated.
with
e as (select equipment.*
from equipment
left join on hierarchy.equipId = equipment.id
where hierarchy.type = 'D')
select e.*
,(select h.attrib_1
from heirarchy h
where h.type = 'A'
start with h.equipId = e.equip_no
connect by PRIOR h.parentEquipId = e.id
) as typeA_attrib_1
,(select h.attrib_2
from heirarchy h
where h.type = 'A'
start with h.equipId = e.equip_no
connect by PRIOR h.parentEquipId = e.id
) as typeA_attrib_2
,(select h.attrib_3
from heirarchy h
where h.type = 'A'
start with h.equipId = e.equip_no
connect by PRIOR h.parentEquipId = e.id
) as typeA_attrib_3
,(select h.attrib_1
from heirarchy h
where h.type = 'B'
start with h.equipId = e.equip_no
connect by PRIOR h.parentEquipId = e.id
) as typeB_attrib_1
,(select h.attrib_2
from heirarchy h
where h.type = 'B'
start with h.equipId = e.equip_no
connect by PRIOR h.parentEquipId = e.id
) as typeB_attrib_2
,(select h.attrib_3
from heirarchy h
where h.type = 'B'
start with h.equipId = e.equip_no
connect by PRIOR h.parentEquipId = e.id
) as typeB_attrib_3
,(select h.attrib_1
from heirarchy h
where h.type = 'C'
start with h.equipId = e.equip_no
connect by PRIOR h.parentEquipId = e.id
) as typeC_attrib_1
,(select h.attrib_2
from heirarchy h
where h.type = 'C'
start with h.equipId = e.equip_no
connect by PRIOR h.parentEquipId = e.id
) as typeC_attrib_2
,(select h.attrib_3
from heirarchy h
where h.type = 'C'
start with h.equipId = e.equip_no
connect by PRIOR h.parentEquipId = e.id
) as typeC_attrib_3
from e