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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T18:37:13+00:00 2026-06-02T18:37:13+00:00

I have a table with the following columns: group_id, parent_id, name In this table

  • 0

I have a table with the following columns: group_id, parent_id, name

In this table parent_id is the group_id of another record. There is a 1 to N relationship of parents to children. This forms a hierarchy with only one top level group having NULL for a parent_id. There could be an arbitrary amount of depth, but in practice my hierarchy is never more than 20 levels deep.

I would like to retrieve every ancestor (a parent’s parent and so on) of a group with a given group_id. I am concerned about the particular way this is returned.

I am using MS SQL 2005, but I am also interested in solutions using other RDBMSs.

I have found some similar questions, but they all seem to break down to recursion, looping or nested sets. I cannot use nested sets, because I cannot change the data structure. I would like to avoid recursion or looping where possible, or at least understand why it is not possible.

Here are some question I found while researching this:

How to select parent ids

Sql recursion without recursion

  • 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-02T18:37:15+00:00Added an answer on June 2, 2026 at 6:37 pm

    The operation is inherently looped. Because each node does not have any finite relation to their root, you must traverse in order to discover it.

    If, for example, you knew that there was a maximum depth of N then you could create N LEFT OUTER JOINs in a single statement and display the last non-null parent ID returned this way.

    The looping requirement is that you simply don’t know what N is, and you cannot ask a declarative language like SQL to “figure it out”

    Even if you can accomplish it with some built-in method, it will still be a loop or recursion, just obfuscated from you.

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

Sidebar

Related Questions

I have a Name table with the columns NameID Name TypeID With the following
I have a table with the following columns: id - INT UNSIGNED AUTO_INCREMENT name
I have a table called trends_points , this table has the following columns: id
I have a mysql table with the following columns: group_id game_id user_id message last_update
Lets say I have table with following columns 1. Client - string. 2. Profit
I have a table with the following columns: EntityId, EntityName, EntityProfile, ................. I want
Lets say I have a table with the following columns: Qty, INTEGER SaleDate, DATETIME
I have a table called tblRentalRates with the following columns: rate_id (int) rate_startdate (datetime)
I have a table [Users] with the following columns: INT SmallDateTime Bit Bit [UserId],
I have a table named with Sales having the following columns: Sales_ID|Product_Code|Zone|District|State|Distributor|Total_Sales Now i

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.