I have a table in my database that maps a management structure like so.
staffid managerid
14 15
13 15
12 15
11 12
10 12
9 10
8 10
7 8
6 8
5 9
4 9
So you can see a kind of chain of command where 10 is the manager of 8 and 9, 9 is the manager of 5 and 4, etc.
I would like to see the management structure from a given id's point for view.
So for example, I want to see from the point of view for 8 and I would expect to see something like this: 6, 7, 8, 10, 12, 15
Or for another example from the point of view of 4, I would see: 4, 9, 10, 12, 15
Does that makes sense? - you would only see people who you are a manager of, who is your manager and who is their manager and so on up the line.
Can someone help with the kind of sql that could produce this list. I think I need some kind of iterative function to pass in a id and loop until I find an id with nobody lower in the chain, and then work back up. I'm just not sure how to get started.
Many thanks.
DS
--------------
Thanks to Jaques for supplying what I eventually used to solve my problem of showing staff in a chain of command, and indeed all other who contributed. Much appreciated.
I have a second part now which has come to light, hopefully someone can help me with this also. So for example, I want to see from the point of view for 8 and I would expect to see something like this below:
A given member of staff should be able to change their own manager id, and any manager id for any staff below them in the chain of command.
So for staff id 8 I am looking for something like this:
staffid managerid change?
6 8 y
7 8 y
8 10 y
10 12 n
12 15 n
15 N/A n
For the purposes of being able to implement this, I really just need the change flag to be 'y' if staffid 8 is the staffid or if 8 is in the chain of command for any staffid's below it. In the same way, for a given id of 10 the table would look like this:
staffid managerid change?
6 8 y
7 8 y
8 10 y
10 12 y
12 15 n
15 N/A n
I hope that is clear enough, and again, many thanks in advance.
DS