?
Solved

get all children in a parent child table

Posted on 2010-11-23
8
Medium Priority
?
1,180 Views
Last Modified: 2012-05-10
i have a table with 3 Fields Name ,ID ,ParentID  I'm looking for sql statement which should give me back all the children id which belong the the parent for example
Name      ID      ParentID
A      1      Null
B      2      1
C      3      1
D      4      2
E      5      2
F      6      3
G      7      3
H      8      9
I       9      8
J      10      8
K      11      8
L      12      10
now i need a sql which i will give (1) and the result should be
2
3
4
5
6
7
this all the children and grandchildren from id 1
0
Comment
Question by:Mr_Ezi
[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
8 Comments
 
LVL 58

Expert Comment

by:cyberkiwi
ID: 34201233
;with cte as (
select id from tbl where id=1
union all
select tbl.id from cte, tbl where tbl.parentID=cte.id)
select id from cte
where id != 1
order by id
0
 

Author Comment

by:Mr_Ezi
ID: 34201453
it only give me back the only id 1
0
 
LVL 58

Expert Comment

by:cyberkiwi
ID: 34201490
Mr Ezi,
Are you sure you are copy-pasting/modifying correctly?
The 2nd to last line specifically says "where id != 1" so it should never return 1, regardless of what else it returns.
0
Learn Veeam advantages over legacy backup

Every day, more and more legacy backup customers switch to Veeam. Technologies designed for the client-server era cannot restore any IT service running in the hybrid cloud within seconds. Learn top Veeam advantages over legacy backup and get Veeam for the price of your renewal

 

Author Comment

by:Mr_Ezi
ID: 34201581
sorry you right i dint copy now go it over it didn't give me back any think just a empty table
0
 
LVL 58

Expert Comment

by:cyberkiwi
ID: 34201626
If you are modifying the query in any significant way, you need to show us the full query you have used. Did you copy and paste verbatim, and are the fields exactly as shown in the question?

The only text that should be changed in the query is where it says "tbl", to your real table name.
0
 
LVL 32

Expert Comment

by:Ephraim Wangoya
ID: 34202746

;WITH CTE(Name, ID, ParentID)
AS
(
      SELECT Name, ID, ParentID
      FROM TABLE1
      WHERE ParentID = 1
      UNION ALL
      SELECT A.Name, A.ID, A.ParentID
      FROM TABLE1 A
      INNER JOIN CTE B ON B.ID = A.ParentID
)
SELECT Name, ID, ParentID
FROM CTE
0
 
LVL 1

Expert Comment

by:_DJ
ID: 34203149

;WITH CTE(ID)
AS
(
      SELECT ID FROM YourTableName WHERE ParentID = 1
      UNION ALL
      SELECT YT.ID FROM YourTableName YT INNER JOIN CTE TC
		 ON TC.ID = YT.ParentID
)

SELECT ID FROM CTE ORDER BY ID

Open in new window

0
 
LVL 4

Accepted Solution

by:
parthmalhan earned 2000 total points
ID: 34203246
Try the following code:

--This will create a temp table and insert values in it
create table #t(name varchar(max),id int,pid int)
insert into #t
select 'muni',1,null union all
select 'yash',2,1 union all
select 'gob',3,1 union all
select 'gop',4,1 union all
select 'ash',5,2 union all
select 'vim',6,2 union all
select 'man',7,3 union all
select 'bab',8,3 union all
select 'nee',9,4 union all
select 'nin',10,4

select * from #t

--real Query
with tree(Child,Text,Parent) as
(
 select id,name,pid from #t
 where pid=1 --put parent id here
 union all
 select z.id,z.name,z.pid from  #t z
 inner join tree on tree.Child=z.pid
)select * from tree

Open in new window

0

Featured Post

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Question has a verified solution.

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

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…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

650 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