Preferiblemente haga eso en la lógica de su aplicación en lugar de en su consulta.
Pseudocódigo
query=SELECT id, type_name, type_reportToId FROM table ORDER BY id ASC, report_toId ASC
// loop through rows
currentParent = 0
processed = 0
hierarachyLevel = 0
while ( processed < foundrows )
{
while ( row = fetchrow( dbResult ) )
{
if ( row[type_reportToId] == currentParent )
{
resultHierarchy[hierarchyLevel] = row
currentParent = row[id]
processed++
hierarchyLevel++
dataseek( dbResult, 0 )
break
}
}