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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T19:17:55+00:00 2026-06-14T19:17:55+00:00

Imagine a Table named Group , each group can have ‘child’ groups as long

  • 0

Imagine a Table named Group, each group can have ‘child’ groups as long as they’re of lower ‘level’. So there is one-to-many relationship between Group and childGroup table.

enter image description here

There is also a many-to-many relation between Group and Items so Groups_Items is used to hold the FK of each table.

I need to write a query where for a given Group key, i should find all its child groups, all the childs of child groups, and all the customers belonging to those found groups.

I know it needs to be a recursive query of some sort but i have no idea how this can be done in SQL. Below is the structure of the tables discussed:

enter image description here

so if i had these data set in the above tables:

enter image description here

And the query was to find For Group 1, find Its customers, Its Child groups (and their Childs) and all their customers, the output should be:

<Group> 1
    <customer> 1
    <customer> 2
    <Group> 2
        <customer> 2
        <Group> 3
        <Group> 4

Can someone please show me how this can be done? thanks

  • 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-14T19:17:58+00:00Added an answer on June 14, 2026 at 7:17 pm

    I hope I understood your question

    ;WITH Hierarchy (GroupID,ID,  ParentID
                   ,hLevel
                   ) AS
    (
       -- Base case
       (
       Select  [Parent Key] as GroupID,NULL,NULL,0
       FROM Tree
       where [Parent Key]=1
       UNION
       SELECT
          [Child Key] as GroupID,
          [Child Key] as ID,
          [Parent Key] as ParentID
          ,1 as hLevel
       FROM Tree
       where [Parent Key]=1
       ) 
    
       UNION ALL
    
       -- Recursive step
       SELECT
          e.[Child Key] as GroupID,
          e.[Child Key],
          e.[Parent Key]
         ,eh.hLevel + 1
       FROM tree e
          INNER JOIN Hierarchy eh ON
             e.[Parent Key] = eh.ID
    )
    
    
    Select Distinct h.GroupID, c.Name,h.ParentID as ParentGroupID
    from Hierarchy h
    left join test c on h.GroupID=c.ID
    

    an older attempt would be

    Declare @Parent int
    Declare @count int
    Select @Parent=??
    
    Select cast(ID as Int) as ID
    into #tmp
    from Tabelle where ParentID=@Parent
    
    select @Count=0
    While @Count<(Select Count(*) from #tmp)
       begin
       Select @Count=(Select Count(*) from #tmp)
       insert into #tmp Select Cast(ID as int) from Tabelle where ParentID in (Select ID from #tmp) and ID not in (Select ID from #tmp)
       end
    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

Imagine you have a normal table view where each row is an item on
Imagine I have following table: NAME DATE OTHER_CONTANT 'A' '2012-06-05' 'baz' 'A' '2012-06-04' 'bar'
Imagine I have this table: Column A | Column B | Column C ------------------------------
Imagine I have a table which stores a series of sparse vectors. A sparse
Imagine I have these columns in a table: id int NOT NULL IDENTITY PRIMARY
Let's imagine that I have to make a table with the following structure with
I have a User and Group model. User has_many Groups and Group belongs_to User
Imagine you have two tables table: name table: keyword id name nameId keyword 1
I have a table named Info of this schema: int objectId; int time; int
Imagine I have a database with one table, this table has only one field

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.