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

  • SEARCH
  • Home
  • 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 3429412
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T07:04:41+00:00 2026-05-18T07:04:41+00:00

Let’s say I’ve got the following table: CREATE TABLE Employees ( EmployeeId int PRIMARY

  • 0

Let’s say I’ve got the following table:

CREATE TABLE Employees
(
EmployeeId int PRIMARY KEY NOT NULL,
ParentEmployeId int REFERENCES Employees(EmployeeId) NULL,
Name varChar(255)
)

All records have a primary identifier and records are able to identify another record as a parent. (My actual schema isn’t about employees, this is just a simplified version for illustration so if you’ve got a better way of handling employee information its not germane to this conversation.)

The following records are inserted:

INSERT INTO Employees VALUES (1, NULL, 'Company President 1')
INSERT INTO Employees VALUES (2, NULL, 'Company President 2')

INSERT INTO Employees VALUES (3, 1, 'Company President 1 - VP')
INSERT INTO Employees VALUES (4, 2, 'Company President 2 - VP')

INSERT INTO Employees VALUES (5, 3, 'Company President 1 - VP - Secretary')
INSERT INTO Employees VALUES (6, 4, 'Company President 2 - VP - Secretary')

INSERT INTO Employees VALUES (7, 5, 'Company President 1 - VP - Secretary - Sandwich Delivery')

These inserts represent:

Company President 1
    Company President 1 - VP
        Company President 1 - VP - Secretary
            Company President 1 - VP - Secretary - Sandwich Delivery
Company President 2
    Company President 2 - VP
        Company President 2 - VP - Secretary

What I’m trying to do is for all employees that have a NULL ParentEmployeeId I want to find the last person in the chain, which in this example would be “Company President 1 - VP - Secretary - Sandwich Delivery” and “Company President 2 - VP - Secretary“.

I’ve got the following CTE which gives me everything including the nesting level but I’m not sure where to go from here. I’d like to avoid cursors if possible.

Also, and this is very important, I have logic elsewhere that guarantees that an employee can only have 1 direct subordinate. So although the schema technically allows for it, Company President 1 will never have two VP’s listed.

WITH EmployeeRec(EmployeeId, ParentEmployeeId, Name, Level) AS
(
    SELECT
        EmployeeId,
        ParentEmployeId,
        Name,
        1 as [Level]
    FROM
        Employees
    WHERE
        ParentEmployeId IS NULL

    UNION ALL

    SELECT
        E.EmployeeId,
        E.ParentEmployeId,
        E.Name,
        R.[Level] + 1
    FROM
        Employees E
    INNER JOIN
        EmployeeRec R
    ON
        E.ParentEmployeId = R.EmployeeId
)
SELECT * FROM EmployeeRec
  • 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-18T07:04:42+00:00Added an answer on May 18, 2026 at 7:04 am

    Keeping track of your master EmployeeID allows you to join the results with the last level to retain the records you need.

    WITH EmployeeRec(Master, EmployeeId, ParentEmployeeId, Name, Level) AS
    (
        SELECT
            [Master] = EmployeeId,
            EmployeeId,
            ParentEmployeId,
            Name,
            1 as [Level]
        FROM
            Employees
        WHERE
            ParentEmployeId IS NULL
    
        UNION ALL
    
        SELECT
            R.Master,
            E.EmployeeId,
            E.ParentEmployeId,
            E.Name,
            R.[Level] + 1
        FROM
            Employees E
        INNER JOIN
            EmployeeRec R
        ON
            E.ParentEmployeId = R.EmployeeId
    )
    SELECT  *
    FROM    EmployeeRec er
            INNER JOIN (
              SELECT  Master, Level = MAX(Level)
              FROM    EmployeeRec
              GROUP BY Master
            ) m ON m.Master = er.Master
                   AND m.Level = er.Level
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say I have the following, declare @A table (a int) insert into @A
Let's say you create a wizard in an HTML form. One button goes back,
Let's say you have a class called Customer, which contains the following fields: UserName
Let's say I have a link in a table like: <td class=ms-vb width=100%> <a
Let's say I have a table that looks something like this: ------------------------------- id|column2|column3 |column4
Let's say I have an int that represents the number of rows in a
Let's say I'm building a data access layer for an application. Typically I have
Let's say we have a simple function defined in a pseudo language. List<Numbers> SortNumbers(List<Numbers>
Let's say I have a drive such as C:\ , and I want to
Let's say I'm writing a PHP (>= 5.0) class that's meant to be a

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.