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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T22:08:11+00:00 2026-06-15T22:08:11+00:00

I have structure like this: <Unit> <SubUnit1> <SubSubUnit1/> <SubSubUnit2/> … <SubSubUnitN/> </SubUnit1/> <SubUnit2> <SubSubUnit1/>

  • 0

I have structure like this:

<Unit>
  <SubUnit1>
           <SubSubUnit1/>
           <SubSubUnit2/>
           ...
           <SubSubUnitN/>
  </SubUnit1/>
  <SubUnit2>
           <SubSubUnit1/>
           <SubSubUnit2/>
           ...
           <SubSubUnitN/>
  </SubUnit2/>
  ...
  <SubUnitN>
           <SubSubUnit1/>
           <SubSubUnit2/>
           ...
           <SubSubUnitN/>
  </SubUnitN/>
</Unit>

This structure has 3 levels: main Unit, SubUnits and SubSubUnits.

I want to select all children by UnitId.
If I search by Unit, I have to get all tree.
If I search by SubUnit1, I have to get SubUnit1 and all children of SubUnit1.
If I search SubSubUnit2, I have to get itself.

Here is my try:

with a(id, parentid, name)
as (
select id, parentId, name
   from customer a
   where parentId is null 
union all
   select a.id, a.parentid, a.Name
   from customer
     inner join a on customer.parentId = customer.id
    )
select parentid, id, name 
from customer pod
where pod.parentid in (
select id
from customer grbs
where grbs.parentid in (
select id
from customer t
where t.parentid = @UnitId
))
union 
select parentid, id, name
from customer grbs
where grbs.parentid in (
select id
from customer t
where t.parentid = @UnitId
)
union
select parentid, id, name
from customer c
where c.Id = @UnitId
order by parentid, id

I use 3 union-words, it is not well but it works. Case structure will have N levels, how I have to get correct result?

  • 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-15T22:08:12+00:00Added an answer on June 15, 2026 at 10:08 pm
    DECLARE @Id int = your_UnitId
    ;WITH cte AS 
     (
      SELECT a.Id, a.parentId, a.name
      FROM customer a
      WHERE Id = @Id
      UNION ALL
      SELECT a.Id, a.parentid, a.Name
      FROM customer a JOIN cte c ON a.parentId = c.id
      )
      SELECT parentId, Id, name
      FROM cte
    

    Demo on SQLFiddle

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

Sidebar

Related Questions

I have a structure like this struct structure { BaseObject &A; //BaseObject has a
I have structure like this. <ul class=gallerylist> <li> <image ...> <input ...> </li> <li>
I have a structure like this: var var1 = { array1 : ['value1','value2', ...],
I have a structure like this.... UITableViewController -> UITableViewCell -> UIView I need the
I have xml structure like this: <Group id=2 name=Third parentid=0 /> <Group id=6 name=Five
My table have data structure like this cate_id task_id date_start date_end other 34 14
The typical Python threadpool will have a structure like this one: def run(self): while
In an ASP.NET MVC3 project I have a structure like this: Core.csproj -> 3rdparty1.dll
I have a files structure like this: Class (folder): - User.php - Rule.php Scripts
I have a file structure like this: dir_a __init__.py mod_1.py mod_2.py dir_b __init__.py my_mod.py

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.