Click here to Skip to main content
16,018,637 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,

I am trying to write a query in ms access 2013 which will output subordinates and subordinates of subordinates and so on for a given manager. Need some hint as I am unable to think of any solution. I am stuck at first level only.
Any help or hint or work around will be of great help. I am using ms access 2013 with c#.net.

table is in the following form. E.g for manager sm1 output will be m1,m2,e1,e2,e3 and for manager m1 output will be e1, e2.

C#
emp	mangr
e1	m1
e2	m1
e3	m2
m1	sm1
m2	sm1
m3	sm2
e4	m3


What I have tried:

this is a simple query to get first level subordinates:

SELECT emp from test where mangr='sm1';

But I am not able to go beyond that. I also need emp from test whose mangr is output of this query.
Posted
Updated 12-Jul-16 11:40am

You need to use subquery together with IN clause. I'd try something like that:

SQL
SELECT emp
FROM Table1 
WHERE mangr IN
    (
     SELECT emp
     FROM Table1
     WHERE mangr='sm1'
     );


For further details, please see: Examples of query criteria - Access[^]
 
Share this answer
 
This can be done is a real SQL engine using a Common Table Expression (CTE).

Access does not have any support for CTEs. I don't know of a way to do this in a single query in Access. You'd have to use VBA code to do it.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900