Domanda

I have a table of employees and their managers:

-- Show the contents of the table
SELECT * FROM ActiveDirectory.dbo.ADUser

Last        First   ManagerDN
Johnson     Brent   CN=Jones\, Bob,OU=IT
Poleman     Debbie  CN=Jones\, Bob,OU=IT
Kismain     Lenni   CN=Jones\, Bob,OU=IT
Braiswool   Marc    CN=Jones\, Bob,OU=IT
Garpial     Pat     CN=Johnson\, Brent,OU=IT
McKinnis    Laurie  CN=Kismain\, Lenni,OU=IT
Thomason    Maddy   CN=Poleman\, Debbie,OU=IT
Dodgers     Kevin   CN=Thomason\, Maddy,OU=IT

I would like to create a list of employees, their managers, and their managers. For simplicity, assume there can be 3 levels:

LastName    FirstName   ManagerDN3                  ManagerDN2                  ManagerDN1
                                                    CN=Braiswool\, Marc,OU=IT   CN=Jones\, Bob,OU=IT
                        CN=Garpial\, Pat,OU=IT      CN=Johnson\, Brent,OU=IT    CN=Jones\, Bob,OU=IT
                        CN=McKinnis\,Laurie,OU=IT   CN=Kismain\, Lenni,OU=IT    CN=Jones\, Bob,OU=IT
                        CN=Thomason\, Maddy,OU=IT   CN=Poleman\, Debbie,OU=IT   CN=Jones\, Bob,OU=IT
Dodgers     Kevin       CN=Thomason\, Maddy,OU=IT   CN=Poleman\, Debbie,OU=IT   CN=Jones\, Bob,OU=IT

Here is the code to create and populate the tables:

-- Create the ADUser table
CREATE TABLE [dbo].[ADUser](
    [Last] [nvarchar](50) NOT NULL,
    [First] [nvarchar](50) NOT NULL,
    [ManagerDN] [nvarchar](50) NOT NULL
)

-- Populate the ADUser table
INSERT INTO [ActiveDirectory].[dbo].[ADUser]
           ([Last]
           ,[First]
           ,[ManagerDN])

SELECT 'Johnson','Brent','CN=Jones\, Bob,OU=IT'
UNION ALL 
SELECT 'Poleman','Debbie','CN=Jones\, Bob,OU=IT'
UNION ALL 
SELECT 'Kismain','Lenni','CN=Jones\, Bob,OU=IT'
UNION ALL 
SELECT 'Braiswool','Marc','CN=Jones\, Bob,OU=IT'
UNION ALL 
SELECT 'Garpial','Pat','CN=Johnson\, Brent,OU=IT'
UNION ALL 
SELECT 'McKinnis','Laurie','CN=Kismain\, Lenni,OU=IT'
UNION ALL 
SELECT 'Thomason','Maddy','CN=Poleman\, Debbie,OU=IT'
UNION ALL 
SELECT 'Dodgers','Kevin','CN=Thomason\, Maddy,OU=IT'

And then I can query each of the columns:

-- Show the Level2 Managers (ManagerDN2)
SELECT Last, First, ManagerDN FROM dbo.ADUser
WHERE ManagerDN = 'CN=Jones\, Bob,OU=IT'

-- Show the Level3 Managers (ManagerDN3)
SELECT Last, First, ManagerDN FROM dbo.ADUser
WHERE ManagerDN IN 
(   SELECT 'CN=' + Last + '\, ' + First + ',OU=IT'
    FROM dbo.ADUser
    WHERE ManagerDN = 'CN=Jones\, Bob,OU=IT'
)

-- Show the Employees
SELECT Last, First, ManagerDN FROM dbo.ADUser
WHERE ManagerDN IN 
(
    SELECT 'CN=' + Last + '\, ' + First + ',OU=IT'
    FROM dbo.ADUser
    WHERE ManagerDN IN 
    (   SELECT 'CN=' + Last + '\, ' + First + ',OU=IT'
        FROM dbo.ADUser
        WHERE ManagerDN = 'CN=Jones\, Bob,OU=IT'
        )
)

How do I efficiently create the list of employees and their managers and their managers

Thanks

È stato utile?

Soluzione

Why don't you have a unique Id for each user?

You could then link to one's manager using foreign key with this Id.

But if schema is set and there is no way to change it, you can look for recursive CTE.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top