This is a tough one... Problem is that there are no actual intermediate results or logic to do this another way.. You actually have to go through the table multiple times ofc. Only thing you could do is maybe count the number of degrees above every employee, and base your logic on this number of hierarchal layers...
Maybe this isn't the best place for this question by the way, have you checked our the ABAP forum?
Sjoerd