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 6922663
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T10:26:07+00:00 2026-05-27T10:26:07+00:00

How would I redesign the below query so that it will recursively loop through

  • 0

How would I redesign the below query so that it will recursively loop through entire tree to return all descendants from root to leaves? (I’m using SSMS 2008). We have a President at the root. under him are the VPs, then upper management, etc., on down the line. I need to return the names and titles of each. But this query shouldn’t be hard-coded; I need to be able to run this for any selected employee, not just the president. This query below is the hard-coded approach.

select P.staff_name [Level1], 
P.job_title [Level1 Title], 
Q.license_number [License 1],
E.staff_name [Level2], 
E.job_title [Level2 Title], 
G.staff_name [Level3], 
G.job_title [Level3 Title]

from staff_view A 
left join staff_site_link_expanded_view P on P.people_id = A.people_id
left join staff_site_link_expanded_view E on E.people_id = C.people_id
left join staff_site_link_expanded_view G on G.people_id = F.people_id
left join facility_view Q on Q.group_profile_id = P.group_profile_id

Thank you, this was most closely matching what I needed. Here is my CTE query below:

with Employee_Hierarchy (staff_name, job_title, id_number, billing_staff_credentials_code, site_name, group_profile_id, license_number, region_description, people_id)
as
(
    select C.staff_name, C.job_title, C.id_number, C.billing_staff_credentials_code, C.site_name, C.group_profile_id, Q.license_number, R.region_description, A.people_id
    from staff_view A
    left join staff_site_link_expanded_view C on C.people_id = A.people_id
    left join facility_view Q on Q.group_profile_id = C.group_profile_id
    left join regions R on R.regions_id = Q.regions_id
    where A.last_name = 'kromer'
)
select  C.staff_name, C.job_title, C.id_number, C.billing_staff_credentials_code, C.site_name, C.group_profile_id, Q.license_number, R.region_description, A.people_id
from staff_view A
left join staff_site_link_expanded_view C on C.people_id = A.people_id
left join facility_view Q on Q.group_profile_id = C.group_profile_id
left join regions R on R.regions_id = Q.regions_id
WHERE C.STAFF_NAME IS NOT NULL
GROUP BY C.STAFF_NAME, C.job_title, C.id_number, C.billing_staff_credentials_code, C.site_name, C.group_profile_id, Q.license_number, R.region_description, A.people_id
ORDER BY C.STAFF_NAME

But I am wondering what is the purpose of the “Employee_Hierarchy”? When I replaced “staff_view” in the outer query with “Employee_Hierarchy”, it only returned one record = “Kromer”. So when/where can we use “Employee_Hierarchy”?

  • 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-27T10:26:08+00:00Added an answer on May 27, 2026 at 10:26 am

    See:

    • SQL Server – Simple example of a recursive CTE
    • MSDN: Recursive Queries using Common Table Expression
    • SQL Server recursive CTE (this seems pretty much like exactly what you are working on!)

    Update:

    A proper recursive CTE consist of basically three things:

    • an anchor SELECT to begin with; that can select e.g. the root level employees (where the Reports_To is NULL), or it can select any arbitrary employee that you define, e.g. by a parameter

    • a UNION ALL

    • a recursive SELECT statement that selects from the same, typically self-referencing table and joins with the recursive CTE being currently built up

    This gives you the ability to recursively build up a result set that you can then select from.

    If you look at the Northwind sample database, it has a table called Employees which is self-referencing: Employees.ReportsTo --> Employees.EmployeeID defines who reports to whom.

    Your CTE would look something like this:

    ;WITH RecursiveCTE AS
    (
        -- anchor query; get the CEO
        SELECT EmployeeID, FirstName, LastName, Title, 1 AS 'Level', ReportsTo
        FROM dbo.Employees
        WHERE ReportsTo IS NULL
    
        UNION ALL
    
        -- recursive part; select next Employees that have ReportsTo -> cte.EmployeeID      
        SELECT 
           e.EmployeeID, e.FirstName, e.LastName, e.Title, 
           cte.Level + 1 AS 'Level', e.ReportsTo
        FROM 
           dbo.Employees e
        INNER JOIN 
           RecursiveCTE cte ON e.ReportsTo = cte.EmployeeID
    )
    SELECT *
    FROM RecursiveCTE
    ORDER BY Level, LastName    
    

    I don’t know if you can translate your sample to a proper recursive CTE – but that’s basically the gist of it: anchor query, UNION ALL, recursive query

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I didn't design this table, and I would redesign it if I could, but
Would having a nice little feature that makes it quicker to write code like
Would it suppose any difference regarding overhead to write an import loading all the
I am writing an application that runs in the background from startup to shutdown,
I would like to take our current mobile website and redesign it for iPhone
I'm doing a custom 404 page for a large website that's undergoing a redesign.
I am starting to redesign and develop a site that contains a lot of
I'm working on a redesign of my website, and would like to make a
Just agreed to a web project, basically redesign from the ground up. Lots of
We are about to commence a redesign of our site and are exploring all

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.