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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T09:28:19+00:00 2026-05-27T09:28:19+00:00

I am trying to define a sql table for groups and subgroups, and so

  • 0

I am trying to define a sql table for groups and subgroups, and so far my design is as follows

groupID groupname       parentgroupID
1       group1          null
2       group2          null
3       subgroup1.1     1
4       subgroup1.1.1   3

I think I am on the right track. Now I would like to retreive data for subgroup 1.1.1 in order to be able to display it in a bussiness tier

group1 > subgroup 1.1 > subgroup 1.1.1

Should I do this in tsql, or I am suppose to do a loop in asp.net in order to get all available parent groups?

This question is both sql and asp.net question so I will post it in both categories.

Thanks for participating

  • 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-05-27T09:28:20+00:00Added an answer on May 27, 2026 at 9:28 am

    You can solve this – provided your on SQL Server 2005 or newer with a recursive CTE:

    -- define a CTE (Common Table Expression) with a name
    ;WITH groups AS
    (
        -- define the "anchor" - those top-level rows
        SELECT 
            GroupID, GroupName, 
            CAST(NULL AS INT) AS 'ParentGroupID', 1 AS 'Level',
            CAST('# ' + GroupName AS VARCHAR(1000)) AS 'Breadcrumb'
        FROM @test
        WHERE ParentGroupID IS NULL
    
        -- recursive join - joins "child" rows to "parent" rows already in the result set       
        UNION ALL
    
        SELECT 
           t2.GroupID, t2.GroupName, t2.ParentGroupID, g.Level + 1 AS 'Level',
           CAST(g.Breadcrumb + ' > ' + t2.GroupName AS VARCHAR(1000))
        FROM @test t2
        INNER JOIN groups g ON t2.ParentGroupID = g.GroupID  -- join back to the CTE
    )
    SELECT * 
    FROM groups
    

    This should give you the results something like this:

    GroupID  GroupName        ParentGroupID  Level  Breadcrumb
      1       group1             NULL          1     # group1
      2       group2             NULL          1     # group2
      3       subgroup1.1         1            2     # group1 > subgroup1.1
      4       subgroup1.1.1       3            3     # group1 > subgroup1.1 > subgroup1.1.1
    

    Any additional formatting should be done in your front-end (web, client/server).

    The Level column is an extra bonus, telling you what level in the hierarchy each row is positioned on.

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

Sidebar

Related Questions

I'm having trouble trying to define the SQL query for this table: There's a
I'm trying to define this SQL schema in JPA: TABLE event (id INT) TABLE
I'm trying to define a table to store student grades for a online report
I am trying to execute a query on a table in my SQL Server
I'm trying to run the following SQL statement: IF OBJECT_ID('MyTable') IS NOT NULL DROP
I am trying to alter a table in SQL server with a script. In
I'm trying to use a trigger defined as follows -- the table CREATE TABLE
Im trying to define a dataTemplate for a business object in my wpf application
I'm trying to define a task that emits (using echo) a message when a
I am trying to define a WCF contract that returns an interface, something like

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.