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

  • Home
  • SEARCH
  • 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 8246359
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T22:28:59+00:00 2026-06-07T22:28:59+00:00

To start this isn’t a duplicate of my other question with the same name,

  • 0

To start this isn’t a duplicate of my other question with the same name, I just couldn’t think of a better name for this one!

I have a SQL table named Player and another called Unit.

  • Each Player MUST belong to a team via a foreign key UnitID.
  • Each Unit can belong to another Team via a recursive field ParentUnitID.
  • A ParentUnit CAN be a ParentUnit (infinite recursion), but a Player can only belong to one Team.
  • A Unit may have many children

So it could be (top down)…

  • TeamA (is the top level)
  • TeamB (belongs to ^^)
  • TeamC (belongs to ^^)
  • TeamD (belongs to ^^)
  • Player_1 (belongs to ^^)

My question is, if I’m given a Player’s PlayerID (the PK for that table), what is the best way to get a specific team?

See my SQLFiddle for the data, structure and a query: http://sqlfiddle.com/#!3/78965/3

With my data (from the fiddle), I want to be able to get every player under “TeamB”, but then “Player4″‘s top unit should be “TeamC”. In order to do that all I want to pass in is the PlayerID and the ID for “TeamB”. So I’m saying “get all players and top units under TeamB and then filtering out all Players except Player4.

EDIT: I believe the above paragraph should read: With my data (from the fiddle), I want to be able to establish the top team(s) that plays below “TeamB”. For each top team below “TeamB” I then want to establish all players that play for or below that team. I then want the ability to limit the list of players to one or more specific players.

As you can see in SQLFiddle I get back multiple rows for each player, I’m sure it’s a quick fix, but I can’t figure it out… That’s what I’m going for in my fiddle, but it’s, well, uh, a bit fiddly… 🙂

Edit: More Information:

OK, so if imagine this is a stored procedure.

  • I pass in PlayerIDs 1,2,3,4
  • I pass in UnitID 2

I would expect the returned data to look something like

Player3, TeamC

Only Player3 is returned as it is the only player which is a descendant of TeamB (ID 2), and TeamC is returned as it is the highest level unit (below UnitID 2) which Player3 belongs to.

If I instead passed in:

  • I pass in PlayerIDs 1,2,3,4
  • I pass in UnitID 6

I would expect

Player1, Team2
Player2, Team2
  • 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-07T22:29:00+00:00Added an answer on June 7, 2026 at 10:29 pm

    This answer has been completely rewritten. The original did not quite work in all circumstances

    I had to change the CTE to represent the full Unit hierarchy for every Unit as a possible root (top unit). It allows a true hierarchy with multiple children per Unit.

    I extended the sample data in this SQL Fiddle to have a player assigned to both units 11 and 12. It properly returns the correct row for each of 3 players that play for a Unit at some level beneath Unit 1.

    The “root” Unit ID and the list of player IDs is conveniently in the outermost WHERE clause at the bottom, making it easy to change the IDs as needed.

    with UnitCTE as (
      select u.UnitID,
             u.Designation UnitDesignation,
             u.ParentUnitID as ParentUnitID,
             p.Designation as ParentUnitDesignation,
             u.UnitID TopUnitID,
             u.Designation TopUnitDesignation,
             1 as TeamLevel
        from Unit u
        left outer join Unit p
          on u.ParentUnitId = p.UnitID
      union all
      select t.UnitID,
             t.Designation UnitDesignation,
             c.UnitID as ParentUnitID,
             c.UnitDesignation as ParentUnitDesignation,
             c.TopUnitID,
             c.TopUnitDesignation,
             TeamLevel+1 as TeamLevel
        from Unit t
        join UnitCTE c
          on t.ParentUnitID = c.UnitID
    )
    select p.PlayerID,
           p.Designation,
           t1.*
      from UnitCTE t1
      join UnitCTE t2
        on t2.TopUnitID = t1.UnitID
       and t2.TopUnitID = t1.TopUnitID
      join Player p
        on p.UnitID = t2.UnitID
     where t1.ParentUnitID = 1
       and playerID in (1,2,3,4,5,6)
    

    Here is a slightly optimized version that has the Unit ID criteria embedded in the CTE. The CTE only computes hierarchies rooted on Units where Parent ID is the chosen Unit ID (1 in this case)

    with UnitCTE as (
      select u.UnitID,
             u.Designation UnitDesignation,
             u.ParentUnitID as ParentUnitID,
             p.Designation as ParentUnitDesignation,
             u.UnitID TopUnitID,
             u.Designation TopUnitDesignation,
             1 as TeamLevel
        from Unit u
        left outer join Unit p
          on u.ParentUnitId = p.UnitID
       where u.ParentUnitID = 1
      union all
      select t.UnitID,
             t.Designation UnitDesignation,
             c.UnitID as ParentUnitID,
             c.UnitDesignation as ParentUnitDesignation,
             c.TopUnitID,
             c.TopUnitDesignation,
             TeamLevel+1 as TeamLevel
        from Unit t
        join UnitCTE c
          on t.ParentUnitID = c.UnitID
    )
    select p.PlayerID,
           p.Designation,
           t1.*
      from UnitCTE t1
      join UnitCTE t2
        on t2.TopUnitID = t1.UnitID
      join Player p
        on p.UnitID = t2.UnitID
     where playerID in (1,2,3,4,5,6)
    


    Here is my original answer. It only works if the Unit Hierarchy is constrained to allow only one child per Unit. The SQL Fiddle example in the question has 3 children for Unit 1, so it falsely returns multiple rows for Players 3, 5 and 6 if run against Unit 1

    Here is a SQL Fiddle that demonstrates the problem.

    with UnitCTE as
      select UnitID,
             Designation UnitDesignation,
             ParentUnitID as ParentUnitID,
             cast(null as varchar(50)) as ParentUnitDesignation,
             UnitID TopUnitID,
             Designation TopUnitDesignation,
             1 as TeamLevel
        from Unit
       where ParentUnitID is null
      union all
      select t.UnitID,
             t.Designation UnitDesignation,
             c.UnitID,
             c.UnitDesignation,
             c.TopUnitID,
             c.TopUnitDesignation,
             TeamLevel+1 as TeamLevel
        from Unit t
        join UnitCTE c
          on t.ParentUnitID = c.UnitID
    )
    select p.PlayerID,
           p.Designation,
           t2.*
      from Player p
      join UnitCTE t1
        on p.UnitID = t1.UnitID
      join UnitCTE t2
        on t2.TopUnitID = t1.TopUnitID
       and t1.TeamLevel >= t2.TeamLevel
      join UnitCTE t3
        on t3.TopUnitID = t1.TopUnitID
       and t2.TeamLevel = t3.TeamLevel+1
     where t3.UnitID = 2
       and playerID in (1,2,3,4)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'll start by saying that this isn't a problem, just curiosity. I have some
Before you start marking this as a duplicate , read me out. The other
I have no idea where to start with this one. I have a confirmation
I have no idea where to start with this one: I have a database
I hope this isn't a duplicate; the other similar questions I read didn't help
This isn't a question but I thought I'd start a thread where links to
I know this isn't strictly a programming question but y'all must have experienced this.
I'm trying to convert this line into Batch how would one start this exe
Let’s start with this statement: We have published couple of free applications on the
I have a situation like this: start(); <Some code> end(); I want start() function

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.