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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T01:53:31+00:00 2026-06-15T01:53:31+00:00

I have a Group table and within that table, there is a ParentId column

  • 0

I have a Group table and within that table, there is a ParentId column that denotes a groups parent within the Group table. The purpose is to build a dynamic menu from these groups. I know I can loop and grab the last child and construct a result set, but I’m curious if there’s a more SQL-y way of accomplishing this.

The table has Id, ParentId and Title fields of int, int and varchar.

Basically, a hierarchy may be constructed this way (People is the base group):

People -> Male   -> Boy
                 -> Man
       -> Female

I want to grab the last child(ren) of each branch. So, {Boy, Man, Female} in this case.

As I mentioned, getting that info isn’t a problem. I’m just looking for a better way of getting it without having to write a bunch of unions and loops where I can basically change the base group and traverse the entire hierarchy outward, dynamically. I’m not really a Db guy, so I don’t know if there’s a slick way of doing this or not.

  • 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-15T01:53:32+00:00Added an answer on June 15, 2026 at 1:53 am

    To get the leaf levels for one of many hierarchies, you can use a Recursive Common Table Expressions (CTEs) to enumerate the hierarchy, and then check which members are not the parent of another group to filter to the leaves:

    Declare @RootID int = 1
    
    ;with cte as (
      select 
        Id,
        ParentId,
        Title
      From
        Groups
      Where
        Id = @RootID
      Union All
      Select
        g.Id, 
        g.ParentId,
        g.Title
      From
        cte c
          Inner Join
        Groups g
          On c.Id = g.ParentID
    )
    Select
      *
    From
      cte g
    Where
      Not Exists (
        Select
          'x'
        From
          Groups g2
        Where
          g2.ParentID = g.Id
      );
    

    You can also do this with a left join rather than a not exists

    http://sqlfiddle.com/#!6/8f1aa/9

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

Sidebar

Related Questions

I have a table that looks like this Group Recipe Priority 0 A 400
I have a table that includes some student group name, lesson time, day names
Basically what I'm trying to do, there are users that have applicable services within
suppose I have SELECT * FROM table t GROUP BY j HAVING condition_one OR
I have the following example in a SQL table Cust Group Sales A 1
I've configured a db w/ a FileStream group and have a table w/ File
I have a table like this: name code group john 12 smith 15 how
I have a table like this one: id group value 1 GROUP A 0.641028
I have a table widget in reporting services where I group rows on a
I have a table of country teams id country group 1 Poland 1 2

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.