Solved

SQL Server - Heirarchy Question

Posted on 2009-04-08
10
182 Views
Last Modified: 2012-05-06
Hello all,

I have a table called "Customer" and this table has a few fields CustID and CustParentID, I need to pull together the heirarchy of the parent and child customers based on a passed custid, if the custparentid is null then it is a top level parent for example:

CustID         CustParentID
123             NULL
234             123
345             234
456             345
789             456

Now I may pass to a proc custif 345.   I want a resultset the best way I can get it to show the display order I am going to show the results of the parent and childs so maybe something returned such as:

CustID        RelationshipOrder      
123            1
234            2
345            3
456            4
789            5

Can anyone help the best way I can do this, or a better way possibly?  

Thanks
0
Comment
Question by:sbornstein2
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 3
10 Comments
 
LVL 60

Accepted Solution

by:
chapmandew earned 500 total points
ID: 24101676
here is one way to do it:

with mycte(custid, custparentid, ordering)
as
(
select custid, custparentid, 1
from #temp
where custparentid is null
union all
select t.custid, t.custparentid , ordering + 1
from #temp t
join mycte c on t.custparentid = c.custid
)

select custid,*  from mycte o
where exists(select 1 from mycte i where custid = 234)
0
 
LVL 60

Expert Comment

by:chapmandew
ID: 24101680
here's the data I loaded:

create table #temp(custid int, custparentid int)

insert into #temp
select 123     ,        NULL
union all
select 234      ,       123
union all
select 345     ,        234
union all
select 456     ,        345
union all
select 789     ,        456
0
 

Author Comment

by:sbornstein2
ID: 24106341
can't this somehow be done in one query or a nested query?  I am testing this now
0
Guide to Performance: Optimization & Monitoring

Nowadays, monitoring is a mixture of tools, systems, and codes—making it a very complex process. And with this complexity, comes variables for failure. Get DZone’s new Guide to Performance to learn how to proactively find these variables and solve them before a disruption occurs.

 

Author Comment

by:sbornstein2
ID: 24106346
this is perfect though and works great, thank you.
0
 
LVL 60

Expert Comment

by:chapmandew
ID: 24106366
glad to help!
0
 

Author Comment

by:sbornstein2
ID: 24106471
actually this won't work only in a perfect situation.  Here is what I had in my test table now.

PartyID          PartyName                        PartyParentID
1      Company ABC      NULL
2      Company DEF      1
3      Company GHI      2
4      Company JKL      3
5      Company MNO      4
6      Jack in the Box 2      7
7      Jack in the Box 1      NULL
8      Jack in the Box 3      1
9      Jack in the Box 4      8
10      Jack in the Box 5      8

You can see now there can be more than one child flowing into a parent or another child for that matter.  So here was my results when passing in 3 as the partyid:

PartyID          PartyName                         PartyParentID   Ordering
1      Company ABC      NULL      1
7      Jack in the Box 1      NULL      1
6      Jack in the Box 2      7      2
2      Company DEF      1      2
8      Jack in the Box 3      1      2
9      Jack in the Box 4      8      3
10      Jack in the Box 5      8      3
3      Company GHI      2      3
4      Company JKL      3      4
5      Company MNO      4      5
0
 

Author Comment

by:sbornstein2
ID: 24106489
i would only want to show the flow up to the parent if I passed in partyid 3, right now this shows all the records.

with mycte(PartyID, OrgName, MacNum, PartyParentID, ordering)
as
(
select PartyID, OrgName, MacNum, PartyParentID, 1
from Organization
where PartyParentID is null
union all
select t.PartyID, t.OrgName, t.MacNum, t.PartyParentID,
       ordering + 1
from Organization t
join mycte c on t.PartyParentID = c.PartyID
)

select PartyID,*  from mycte o
where exists(select 1 from mycte i where PartyID = 3)
0
 

Author Comment

by:sbornstein2
ID: 24106494
ignore the macnum field in that above
0
 

Author Comment

by:sbornstein2
ID: 24106507
one thing is I am not sure what ordering would be right if more than one company had the same parent.  I need to find that out.
0
 

Author Comment

by:sbornstein2
ID: 24106671
i guess for now is there a way to do this and just order them based on the order of the partyid ascending I guess if there is more than one with the same parent?  Just a thought
0

Featured Post

Backup Solution for AWS

Read about how CloudBerry Backup fully integrates your backups with Amazon S3 and Amazon Glacier to provide military-grade encryption and dramatically cut storage costs on any platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
java mysql insert application 14 46
Generate Unique ID in VB.NET 21 104
Replication failure 1 23
How come this XML node is not read? 3 31
by Mark Wills Attending one of Rob Farley's seminars the other day, I heard the phrase "The Accidental DBA" and fell in love with it. It got me thinking about the plight of the newcomer to SQL Server...  So if you are the accidental DBA, or, simp…
When table data gets too large to manage or queries take too long to execute the solution is often to buy bigger hardware or assign more CPUs and memory resources to the machine to solve the problem. However, the best, cheapest and most effective so…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question