?
Solved

SQL Query resursive - get parent records

Posted on 2007-10-19
10
Medium Priority
?
893 Views
Last Modified: 2008-05-13
Hi All,

consider sql table have following columns - records,

id         name         parent_id
1          mem          2
2          mem          3
3          mem          4
4          mem          0
5         sel              6
6         sel              0

i want to get all related parents for record id=1 means ,, in one query i want to get records 1,2,3,4 but note 5-6 because they are not related to record 1 ..






0
Comment
Question by:Abu Hamdan
  • 3
  • 3
  • 2
  • +1
10 Comments
 
LVL 10

Expert Comment

by:answer_me
ID: 20112298
begin
declare @temptable table
(
id  int,
name  nvarchar(200),
parent int
)
declare @searchid int
set @searchid =<yourid>

while (1=1)
begin
insert into @temptable
select * from yourtable where id =@searchid
if @@rowcount=0
break
select top 1 @searchid=parent from @temptable where parent =@searchid
end
select * from @temptable
end
0
 
LVL 17

Expert Comment

by:Chris Mangus
ID: 20112308
More realistically...the data would look like this:

id         name         parent_id
1          mem          0
2          mem          1
3          mem          2
4          mem          3
5         sel              6
6         sel              0

And a query to get the relationships would be:

SELECT    P1.[ID], P2.[ID]
FROM         dbo.myTable AS P1
INNER JOIN dbo.myTable AS P2 ON P1.[id] = P2.parent_id
0
 
LVL 17

Expert Comment

by:Chris Mangus
ID: 20112328
Oops...

More realistically...the data would look like this:

id         name         parent_id
1          mem          0
2          mem          1
3          mem          1
4          mem          1
5         sel              6
6         sel              0

And a query to get the relationships would be:

SELECT    P1.[ID], P2.[ID]
FROM         dbo.myTable AS P1
INNER JOIN dbo.myTable AS P2 ON P1.[id] = P2.parent_id

In your sample data, records 3 and 4 don't necessarily relate to record 1, unless you have a multi-level heirarchy and not just a 2 level heirarchy.
0
Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

 
LVL 17

Expert Comment

by:Chris Mangus
ID: 20112389
If your data does have more than 2 heirarchies, and is like you originally posted, the following query will pull it out:

SELECT  P1.[ID] As Parent, P2.[ID] As Child1, P3.[ID] As Child2
FROM dbo.myTable AS P1
LEFT OUTER JOIN dbo.myTable AS P2 ON P1.[ID] = P2.Parent_ID
LEFT OUTER JOIN dbo.myTable AS P3 ON P2.[ID] = P3.Parent_ID
Go
0
 
LVL 35

Expert Comment

by:James0628
ID: 20113080
What makes the first 4 records "related parents".  In your example, it seems to be that name is "mem".  If you're looking for all records with the same name as id 1, something like the following should work:

select * from table
where name = (select name from table where id = 1)


 James
0
 
LVL 35

Expert Comment

by:James0628
ID: 20113083
Oops.  Just to be clear, that first sentence was a question.

 What makes the first 4 records "related parents"?

 James
0
 
LVL 7

Author Comment

by:Abu Hamdan
ID: 20113923
Hello,,

Its mutiple level heirarchy ,, i mean that there is record id=1 ,, and record one has parent id as record 2 ,,, then record 2 has parent id as 3 ,, then 3 has parent id as 4 ,, then 4 has no parent id ,, then just get these 4 records .. and discard other ,, and the query should be something like this:

select (all -level-parents) from table where id=1 ,,

and result as

1          mem          2
2          mem          3
3          mem          4
4          mem          0

-----

and if i want parents for record 5 same query but result should be as:

5         sel              6
6         sel              0

Also i know how to do it as stored procs -- but i am looking to do as query if possible






0
 
LVL 35

Expert Comment

by:James0628
ID: 20127745
Ah.  Then I guess you're going to have to look at some of the other suggestions.  Is there any limit on how many levels you can have?  That will probably be a key factor.

 James
0
 
LVL 7

Author Comment

by:Abu Hamdan
ID: 20129010
Hi,

No actually there isnt ,, maybe for one record there will be 1 parent or 10 parent
0
 
LVL 10

Accepted Solution

by:
answer_me earned 1500 total points
ID: 20133918
If there is any max limit of number of parent then its possible ....
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…
Suggested Courses

807 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