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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T00:27:40+00:00 2026-06-03T00:27:40+00:00

Given records within a table have parents within the same table and those parents

  • 0

Given records within a table have parents within the same table and those parents have their own parents, what query will get me the records for all parents of a requested record provided that different records can have different number of parents.

Example: I have id of record 4 and want to get all the parent records (1,2,3). Of course I can get record 4, see who is the parent and go get record 3, see who’s the parent and go get record 2, until the record I get has no parents, but is there a single query that can get me all the records?

Example Table:
ID | Type | Parent ID
----------------------------
1  |Master|
2  |Sub 1 | 1
3  |Sub 2 | 2
4  |Sub 3 | 3
5  |Master| 
6  |Sub 1 | 5
  • 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-03T00:27:41+00:00Added an answer on June 3, 2026 at 12:27 am

    It is possible to represent the hierarchy if you assumed a fixed, maximum level of depth. For example, in this sample, I assume no more than four levels of depth. Without common-table expressions for recursive evaluation, there is no single query means beyond this type of solution or using a variable.

    Select T1.Id, T1.Type
        , Concat( Coalesce( Concat(Cast(T4.Id As char(10)),','),'')
            , Coalesce( Concat(Cast(T3.Id As char(10)),','),'')
            , Coalesce( Concat(Cast(T2.Id As char(10)),','),''))
            As Hierarchy
    From ExampleTable As T1
        Left Join ExampleTable As T2
            On T2.Id = T1.ParentId
        Left Join ExampleTable As T3
            On T3.Id = T2.ParentId
        Left Join ExampleTable As T4
            On T4.Id = T3.ParentId
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given a table action(start:DATE, length:NUMBER, type:NUMBER), with all records unique, I need to select
I have a table that contains records with a column indicates the Date. Given
I am selecting records from my table that have been posted within the past
I have the next query for getting addresses within a given distance and given
I have a fairly small corpus of structured records sitting in a database. Given
Having tried several solutions to get NHibernate to delete orphan records. Given the following
I have a table with about 1000 records and 2000 columns. What I want
I have a table of over 1 million test score records that basically have
I have a table, Sheet1$ that contains 616 records. I have another table, Rates$
I have a rather simple PL/SQL query (...) DECODE(upper(Table.Column), 'SOMETHING', '---', TABLE2.COLUMN2) as VALUE

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.