SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


return parent child relation


return parent child relation

Author
Message
vishnurajeshsat
vishnurajeshsat
Forum Newbie
Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)

Group: General Forum Members
Points: 7 Visits: 24
PRNT CHLD POS
1050000 1000000 1
1050000 5000000 2
1050001 1000004 1
1050001 5000002 2
..............................................
..............................................
1000000 1010000 1
1000000 1010001 2
1000000 3030000 3
..............................................
..............................................
1010000 2020000 1
1010001 2030000 1
....................................... ......
.............................................

IN THE ABOVE TABLE EACH PARENT HAS A CHILD MAY BE MORE THAN ONE. AGAIN CHILD BECOME PARENT NOT ALL THE CHILD AND IT WILL GOS ON UPTO SOME LEVEL THEN CHILD WONT BE A PARENT.
CURRENTLY I AM USING BELOW QUERY
select * from TABLE_NAME where
PRNT
in
('1050000',
'1000000',
'1010000',
'1010001')
order by case
PRNT
when '1050000' then 1
when '1000000' then 2
when '1010000' then 3
when '1010001' then 4
end , POS ASC

AND RESULT WILL BE

PRNT CHLD POS
1050000 1000000 1
1050000 5000000 2
1000000 1010000 1
1000000 1010001 2
1000000 3030000 3
1010000 2020000 1
1010001 2030000 1

IS THERE ANY OTHER WAY TO GET THE ABOVE RESULTS USING THE 1ST PARENT CODE(1050000) TO RETURN UP TO THE LAST CHILD(2030000)
Sean Lange
Sean Lange
SSC Guru
SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)

Group: General Forum Members
Points: 62755 Visits: 17959
I believe a recursive cte is what you are looking for. If you want some more specific help you need to post some details. Take a look at the first link in my signature about best practices for how to post questions in such a way that it will generate a response.

_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Modens splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Conficker
Conficker
SSC Veteran
SSC Veteran (245 reputation)SSC Veteran (245 reputation)SSC Veteran (245 reputation)SSC Veteran (245 reputation)SSC Veteran (245 reputation)SSC Veteran (245 reputation)SSC Veteran (245 reputation)SSC Veteran (245 reputation)

Group: General Forum Members
Points: 245 Visits: 386
Agree with Sean,

Recursive CTE like below,

WITH Tree AS
(

SELECT Parent.ID [ID]
, Parent.ParentID [ParentID]
FROM Entity Parent
WHERE Parent.ID = @Parammeter

UNION ALL

SELECT Child.ID
, Child.ParentID
FROM Entity Child
INNER JOIN Tree ON Child.ParentID = Tree.ID
)


SELECT *
FROM Tree


vishnurajeshsat
vishnurajeshsat
Forum Newbie
Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)

Group: General Forum Members
Points: 7 Visits: 24
Dear Sean Lange & SSC Rookie,

Thanks for your reply.I cant able to show my table in this forum. Can you tell me how to add image in this forum then i will explain clearly that what i am really struggling.
Sean Lange
Sean Lange
SSC Guru
SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)

Group: General Forum Members
Points: 62755 Visits: 17959
vishnurajeshsat (8/2/2012)
Dear Sean Lange & SSC Rookie,

Thanks for your reply.I cant able to show my table in this forum. Can you tell me how to add image in this forum then i will explain clearly that what i am really struggling.


Why can't you share the table structure? If it is because of not wanting to share your real structure just make something up that is close and only has the columns you need. Change the names and such to protect yourself. Then post some sample data. Again change values and do what you need to protect the real data. Everything you can do to make something close to your real issue will help.

An image is not really going to provide much to allow us to help you with the code.

_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Modens splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Sean Lange
Sean Lange
SSC Guru
SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)

Group: General Forum Members
Points: 62755 Visits: 17959
You posted a bunch of stuff in your first post that somewhat looks like it came from your table. Turn that into something we can use and we can help.

Turn that stuff into a create table statement and some insert statements for the data.

_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Modens splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Lowell
Lowell
SSC Guru
SSC Guru (71K reputation)SSC Guru (71K reputation)SSC Guru (71K reputation)SSC Guru (71K reputation)SSC Guru (71K reputation)SSC Guru (71K reputation)SSC Guru (71K reputation)SSC Guru (71K reputation)

Group: General Forum Members
Points: 71974 Visits: 40942
an example of what Sean is asking you for: a CTE or CREATE TABLE with his first post:

With MyCTE (PRNT,CHLD,POS)
AS
(
SELECT '1050000','1000000','1' UNION ALL
SELECT '1050000','5000000','2' UNION ALL
SELECT '1050001','1000004','1' UNION ALL
SELECT '1050001','5000002','2' UNION ALL
SELECT '1000000','1010000','1' UNION ALL
SELECT '1000000','1010001','2' UNION ALL
SELECT '1000000','3030000','3' UNION ALL
SELECT '1010000','2020000','1' UNION ALL
SELECT '1010001','2030000','1'
)

select * from MyCTE where
PRNT
in
('1050000',
'1000000',
'1010000',
'1010001')
order by case
PRNT
when '1050000' then 1
when '1000000' then 2
when '1010000' then 3
when '1010001' then 4
end , POS ASC




Lowell
--help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!
Conficker
Conficker
SSC Veteran
SSC Veteran (245 reputation)SSC Veteran (245 reputation)SSC Veteran (245 reputation)SSC Veteran (245 reputation)SSC Veteran (245 reputation)SSC Veteran (245 reputation)SSC Veteran (245 reputation)SSC Veteran (245 reputation)

Group: General Forum Members
Points: 245 Visits: 386
Here is All you want, with dummy data,

CREATE TABLE #Test
(
ID INT,
ParentID INT,
TName VARCHAR(25)
)
INSERT INTO #Test
(
ID,
ParentID,
TName
)

SELECT 1,NULL,'Granfather'
UNION ALL
SELECT 2,1,'father1'
UNION ALL
SELECT 3,1,'father2'
UNION ALL
SELECT 4,2,'1st-son-father1'
UNION ALL
SELECT 5,2,'2nd-son-father1'
UNION ALL
SELECT 6,3,'1st-son-father2'
UNION ALL
SELECT 7,3,'2nd-son-father2'


SELECT * FROM #Test

--------------------------------------------
-- Recursive CTE
;WITH Tree AS
(

SELECT Parent.ID [ID],
Parent.ParentID [ParentID],
Parent.TName
FROM #Test Parent
WHERE Parent.ID =2--@ID -- Pass the Top Id you want to see children of it.

UNION ALL

SELECT Child.ID ,
Child.ParentID,
Child.TName
FROM #Test Child
INNER JOIN Tree ON Child.ParentID = Tree.ID
)


SELECT *
FROM Tree


DROP TABLE #Test


Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search