We help IT Professionals succeed at work.
Get Started

SQL - Ordering Supervisor Hierarchy

nightshadz
nightshadz asked
on
102 Views
Last Modified: 2017-04-26
I have a supervisor table with three columns which looks similar to the one below. I need to reformat the reporting structure in a specific format which is defined below. It basically starts out at the top level and any direct report to that person is -1.  Hope this makes sense. Thanks in advance!

NAME        SUPERVISOR_ID        REPORTS_TO_ID
Person1     2                                 1
Person2     12                               2
Person3     37                               12
Person4     62                               37
Person5     154                             37
Person6     144                             2
Person7     168                             144
Person8     69                               168

Open in new window


How can I write a query to output the structure so it looks like this:
Person1 = “CEO -0”
   Person6 = “CEO -1”
      Person7 = “CEO -2”
         Person8 = “CEO -3”
   Person2 = “CEO -1”
      Person3 = “CEO -2”
        Person4 = “CEO -3”
      Person5 = “CEO -3”
Comment
Watch Question
Database and Application Developer
Commented:
This problem has been solved!
Unlock 1 Answer and 2 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE