Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 808615
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T00:35:23+00:00 2026-05-15T00:35:23+00:00

I need to generate a list of users that are managers, or managers of

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T00:35:24+00:00Added an answer on May 15, 2026 at 12:35 am

    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
            )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a List<Polyline> that I need to generate in a second thread so
I have an html file and i need to generate a list of all
I need to run a script that will generate the projects.list file when a
Hello :) I realy need your help here. I dynamically generate list of items
What do you do if you need to generate a read-only list of data
I need to have a list of links generated by JSF and displayed in
I need to generate report that will show number of new / changed rows
We have a website that contains a database of places. For each place our
I have 2 tables in a mysql database : users and pets . In
I have 7 summary tables one for each day of the week. txn_summary_monday...txn_summary_tuesday and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.