views:

64

answers:

1

I need to generate a list of users that are managers, or managers of managers, for company departments.

I have two tables; one details the departments and one contains the manager hierarchy (simplified):

CREATE TABLE [dbo].[Manager](
[ManagerId] [int],
[ParentManagerId] [int])

CREATE TABLE [dbo].[Department](
[DepartmentId] [int],
[ManagerId] [int])

Basically, I'm trying to build a CTE that will give me a list of DepartmentIds, together with all ManagerIds that are in the manager hierarchy for that department.

So... Say Manager 1 is the Manager for Department 1, and Manager 2 is Manager 1's Manager, and Manager 3 is Manager 2's Manager, I'd like to see:

DepartmentId, ManagerId
1, 1
1, 2
1, 3

Basically, managers are able to deal with all of their sub-manager's departments.

Building the CTE to return the Manager hierarchy was fairly simple, but I'm struggling to inject the Departments in there:

WITH DepartmentManagers
AS
(
    SELECT      ManagerId,
                ParentManagerId,
                0 AS Depth
    From        Manager

    UNION ALL

    SELECT      Manager.ManagerId,
                Manager.ParentManagerId,
                DepartmentManagers.Depth + 1 AS Depth
    FROM        Manager
    INNER JOIN  DepartmentManagers
                ON DepartmentManagers.ManagerId = Manager.ParentManagerId
)

I need a list of all Departments together with all related Managers.

Can anyone help?

+1  A: 

Change your anchor query:

WITH    DepartmentManagers
        AS
        (
        SELECT  d.DepartmentID,
                d.ManagerId,
                m.ParentManagerId
                0 AS Depth
        FROM    Department d
        JOIN    Manager m
        ON      m.ManagerID = d.managerID
        WHERE   DepartmentID = 1
        UNION ALL
        SELECT  d.DepartementID,
                m.ManagerId,
                m.ParentManagerId,
                d.Depth + 1
        FROM    DepartmentManagers d
        JOIN    Manager m
        ON      m.ManagerId = d.ParentManagerID
        )
Quassnoi
Sorry - I was a little unclear on exactly what I need the results to contain. I want to list all departments with all of their managers - this will form the basis of a view.Thanks
Milky Joe
@Milky: see the post update.
Quassnoi
Ah. This doesn't appear to work where a manager is a manager of more than 1 department. I'm discovering that the data I'm dealing with is pretty bad: a manager can also be their own manager!? I've updated my post to reflect this.
Milky Joe
@Milky: could you please express the problem more definitely than "doesn't appear to work?" The best way would be to update your post with a sample recordset and desired results.
Quassnoi
I've marked your response as the answer. To be fair to you, you've answered my original question perfectly. It's just that, after digging deeper, the data is a little more complex. It looks like I'll need to flesh out the manager hierarchy stuff a little more.Cheers for your help!
Milky Joe