Suppose I have a table that describes primary and secondary reporting lines for each member of staff. Let's imagine an organizational structure where the CEO, employee 0, has 2 managers (1 and 2) reporting to him.
Manager 2 has 2 staff in her team (3 and 4), however staff member 4 actually works in Manager 1's timezone, so while he has 2 as his primary report, he also reports to Manager 1 as a Secondary report so that 1 can fullfil normal fiduciary managerial obligations (provide support, etc.).
In addition to taking a secondary management role for employee 4, Manager 2 also has a team member reporting to him (5).
Edit: To illustrate the multi-parent problem, let's give team member 4 an intern, staff member 6. Team member 6 is now the subordinate of both managers 1 and 2 - the latter being inherited through the secondary reporting line.
The organizational structure would look like this:
+--+-------+---------+
|ID|Primary|Secondary|
|0 |NULL |NULL |
|1 |0 |NULL |
|2 |0 |NULL |
|3 |1 |NULL |
|4 |1 |2 |
|5 |2 |NULL |
|6 |4 |NULL |
+--+-------+---------+
Now I want to expand this into a SQL view that gives me a list of people below any given staff member, covering both primary and secondary reports. So for staff member 2 (the manager with a primary and secondary report), I would expect to see team members 4 and 5, and for the CEO (0) I'd expect to see ever staff member other than the CEO. Our new intern, 6, is the subordinate of the CEO, managers 1 and 2, as well as his direct manager, 4.
This would look like this:
+--+-----------+
|ID|Subordinate|
|0 |1 |
|0 |2 |
|0 |3 |
|0 |4 |
|0 |5 |
|0 |6 |
|1 |3 |
|1 |4 |
|1 |6 |
|2 |4 |
|2 |5 |
|2 |6 |
|4 |6 |
+--+-----------+
How would I achieve this in SQL? I'm thinking some kind of OUTER APPLY operation on the ID but I'm struggling to get my head around the reentrancy that would be required (I think) to solve this. My background is in procedural programming, which I think is part of the reason I'm struggling here.
NB: An obvious question that I'd like to anticipate here is "Surely this is an XY problem - why on earth would you want to do this?"
I want to use row-level security in PowerBI to give each staff member access to certain information about individuals below them in the organizational structure. Unfortunately, RLS doesn't permit the execution of stored procedures per individual, so I'm stuck with doing this combinatorial expansion and then simply filtering the above table based on the login.
Having said that, I'm open to better ways of approaching this problem.