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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T05:47:51+00:00 2026-06-14T05:47:51+00:00

I have a table that store recursive records through two fields: ID and PARENTID.

  • 0

I have a table that store recursive records through two fields: ID and PARENTID.

I have a functionality that can associate a parent to an element of the tree. When I select the elements that can be “parent” of myself I shall obviously exclude from the resulting list all the elements which, directly or indirectly, depends on me but also the elements from which I already depend.

Let’s make an example. Given the following sample hierarchy:

ID                  PARENT_ID
-----------         ------------------
1                    NULL
2                    1
3                    NULL
4                    2
5                    1
6                    3

If I would like to find the elements that can be parent of element with ID = 4 I shall consider only elements 5 - 3 - 6 because they do not have any relation with the actual structure.

How can I get those elements with a CTE query?

  • 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-14T05:47:54+00:00Added an answer on June 14, 2026 at 5:47 am
    Select *
    into #tmp
    From Tree2
    
    ;WITH Rollups AS (
        SELECT ID, Parent_Id
        FROM tree2 where ID=4
        UNION ALL
        SELECT parent.Id, parent.Parent_Id
        FROM tree2 parent 
        INNER JOIN Rollups child ON child.Id = parent.Parent_Id
    )
    Delete #tmp from Rollups where #tmp.ID=Rollups.ID
    
    ;WITH Rollups AS (
        SELECT ID, Parent_Id
        FROM tree2 where ID=4
        UNION ALL
        SELECT parent.Id, parent.Parent_Id
        FROM tree2 parent 
        INNER JOIN Rollups child ON child.Parent_Id = parent.Id
    )
    Delete #tmp from Rollups where #tmp.ID=Rollups.ID
    
    Select * from #tmp
    Drop Table #tmp 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a mysql table that has two fields that store the same type
I have a standard store schema with an InvoiceLine table that contains Products. I'd
I have a table that need to store Timeslot such as 10:00 AM PST,
I have a mysql database table that will store locations for buildings and events
I have one table that I'm using to store all access requests for a
I have a table with timeslots that users can sign up for. On the
I have oracle table that holds more than 30 million records, I need to
I have a table that needs each row to store an ordered collection of
I have three columns in mysql table: tax_id, company_name, store_name. I need that tax_id
I have a table that stores data that has been entered regarding the amount

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.