Sql to order user-managers so that the highest manager appears first

9 views
Skip to first unread message

Anoop

unread,
Nov 16, 2007, 2:03:40 AM11/16/07
to sql-users
I have a typical user-manager table, where a manager can have direct
reports under him and be a direct report to another manager as well.
How can I sort the table (using sql order by or something) so that the
hierarchy is maintained in the query result?

For example: my table contains direct report - manager relationship
like: (user1 has no manager - is the top: CEO or something)

direct-report manager
--------------------+------------------------------
user7 user1
user2 user5
user3 user4
user4 user6
user5 user6
user6 user7
user1

what I want would be:

user1
user7 user1
user6 user7
user5 user6
user4 user6
user3 user4
user2 user5


Thanks.
Reply all
Reply to author
Forward
0 new messages