I have the need to return tree like results from a single table. At the moment im using connect by and start with to ensure that all the correct results are returned.
select id,parent_id
from myTable
connect by prior id = parent_id start with name = 'manager'
group by id, parent_id
order by parent_id asc
However i want the results to return in tree structure. So each time a parent row is found its children rows will be displayed directly underneath it. Then move onto next parent and do the same
Expected results
- Parent A
- child a
- child b
- Parent B
- child c
- child d
Actual results
- Parent A
- Parent B
- child a
- child b
- child c
- child d
Is it possible to do this in oracle? My table uses a parent_id field to identify when a row has a parent. Every row also has a sort order, which is the order it should be sorted under its parent and a unique Id.
I’m using an Oracle DB
What you want is to use
ORDER SIBLINGS BY. The query you have is ordering by theparent_idcolumn which is overriding any hierarchical ordering.The query below should do what you need it to do: