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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T19:03:04+00:00 2026-06-11T19:03:04+00:00

Say I have the following tree: CREATE TABLE Tree(Id int, ParentId int, Name varchar(20),

  • 0

Say I have the following tree:

CREATE TABLE Tree(Id int, ParentId int, Name varchar(20), Level int)

INSERT INTO Tree(Id, ParentId, Name, Level)
SELECT 1, NULL, 'Bob', 0 UNION ALL 
SELECT 2, 1, 'John', 1 UNION ALL
SELECT 3, 1, 'Bill', 1 UNION ALL
SELECT 4, 3, 'Peter', 2 UNION ALL
SELECT 5, 4, 'Sarah', 3

I need a script to find the ancestor of a given node at a specific level:

For example the ancestor of Sarah at level 2 is Peter, and the ancestor of Peter at level 0 is Bob.

Is there an easy way to do this using a hierarchical CTE? sqlfiddle

  • 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-06-11T19:03:05+00:00Added an answer on June 11, 2026 at 7:03 pm

    You can do this:

    ;WITH TreeCTE
    AS
    (
        SELECT Id, ParentId, Name, Level
        FROM Tree
        WHERE Id = 5
        UNION ALL 
        SELECT t.Id, t.ParentId, t.Name, t.Level
        FROM TreeCTE AS c
        INNER JOIN Tree t ON c.ParentId = t.Id  
    )
    SELECT * FROM TreeCTE;
    

    This will give you the parent-child chain beginning from Sarah with the id = 5 ending with the top most parent with the parentid = NULL.

    Here is the updated sql fiddle

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

Sidebar

Related Questions

Say we have the following structure: struct Tree { string id; int numof_children; Tree
Say that have the following CTE that returns the level of some tree data
Let's say I have following table like this <!DOCTYPE html PUBLIC -//W3C//DTD XHTML 1.0
Say I have the following chef role: name test description role for test run_list
Say I have the following data IEnumerable<IEnumerable<int>> items = new IEnumerable<int>[] { new int[]
Lets say I have a following structures in c++ struct Fruit { std::string name,
lets say i have a tree of the following structure: <div>node level1 <div>node level2
Let's say I have a class which will be used to create either tree
Say I have the following Penn Tree: (S (NP-SBJ the steel strike) (VP lasted
Say i have the following table, using Oracle 10g ARTIFACT_LABEL | DEPENDANT_ON test1 |

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.