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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T17:07:35+00:00 2026-06-13T17:07:35+00:00

Possible Duplicate: Simplest way to do a recursive self-join in SQL Server? I have

  • 0

Possible Duplicate:
Simplest way to do a recursive self-join in SQL Server?

I have to create a table in SQL that will comprise of groups of items/products. Each new group made will be made under one of the pre-defined groups or the groups previously formed. I want to keep all this data in a SQL Table. So far, I have though of creating a table like this:

  • Group ID
  • Group Name
  • Group Under (This will store the ID of the group under which this group is from

But this can only refer to just the next level, how will I get to know who is the super-parent of this group.

For example:

  • I have groups A, B, C.
  • A has further subgroups A1, A2, A3.
  • A1 has further subgroups, A11, A12, A13.

I will I have the information about super-parent group i.e A from A11 or A22 or A33?

Let me know if the problem is not clear..

  • 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-13T17:07:36+00:00Added an answer on June 13, 2026 at 5:07 pm

    Assuming T-SQL and MSSQLServer (you didn’t specify), and given that your Group table should look something like this:

    Id | Name | ParentId
    ---+------+---------
     1 | A    | NULL
     2 | B    | NULL
     3 | C    | NULL
     4 | A1   | 1
     5 | A2   | 1
     6 | A3   | 1
     7 | A11  | 4
     8 | A12  | 4
     9 | A13  | 4
    

    You can use the following recursive CTE to find the top level a given group, say ‘A12’:

    WITH [Group](Id, Name, ParentId) AS
    (
        SELECT 1, 'A'  , NULL UNION
        SELECT 2, 'B'  , NULL UNION
        SELECT 3, 'C'  , NULL UNION
        SELECT 4, 'A1' , 1    UNION
        SELECT 5, 'A2' , 1    UNION
        SELECT 6, 'A3' , 1    UNION
        SELECT 7, 'A11', 4    UNION
        SELECT 8, 'A12', 4    UNION
        SELECT 9, 'A13', 4
    
    ), q AS 
    (
        SELECT  
            *
        FROM    
            [Group]
        WHERE
            [Name] = 'A12' -- Given 'A12' as the child
        UNION ALL
        SELECT  
            g.*
        FROM
            [Group] g
        JOIN    
            q
            ON      
            q.ParentId = g.Id
        )
    SELECT  
        *
    FROM
        q
    WHERE
        ParentId IS NULL
    

    This query returns:

    Id | Name | ParentId
    ---+------+---------
     1 | A    | NULL
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Can’t create handler inside thread that has not called Looper.prepare() inside AsyncTask
Possible Duplicate: Simplest way to profile a PHP script We are building this online
Possible Duplicate: Extracting dollar amounts from existing sql data? I have a column in
Possible Duplicate: What's the simplest way to parse URL parameters in Clojure? I'd like
Possible Duplicate: How to create comma separated list from array in PHP? I have
Possible Duplicate: MySQL check if a table exists without throwing an exception I have
Possible Duplicate: Building a simple mp3 music player I want to create simplest mp3
Possible Duplicate: Simplest way to profile a PHP script Sometimes my website loads slowly
Possible Duplicate: nth-child for every two table rows I'm trying to work my way
Possible Duplicate: Simplest way to increment a date in PHP? I am very new

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.