Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

return parent child relation Expand / Collapse
Author
Message
Posted Tuesday, July 31, 2012 4:52 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Friday, September 27, 2013 4:57 AM
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)
Post #1337779
Posted Tuesday, July 31, 2012 8:24 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 8:30 AM
Points: 13,480, Visits: 12,333
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 Moden's 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)
Post #1337935
Posted Wednesday, August 1, 2012 8:21 AM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Tuesday, June 3, 2014 4:20 AM
Points: 69, Visits: 363
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

Post #1338574
Posted Thursday, August 2, 2012 5:00 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Friday, September 27, 2013 4:57 AM
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.
Post #1339056
Posted Thursday, August 2, 2012 8:36 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 8:30 AM
Points: 13,480, Visits: 12,333
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 Moden's 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)
Post #1339221
Posted Thursday, August 2, 2012 8:39 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 8:30 AM
Points: 13,480, Visits: 12,333
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 Moden's 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)
Post #1339224
Posted Thursday, August 2, 2012 8:43 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 8:32 AM
Points: 12,918, Visits: 32,086
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

--There is no spoon, and there's no default ORDER BY in sql server either.
Actually, Common Sense is so rare, it should be considered a Superpower. --my son
Post #1339228
Posted Thursday, August 2, 2012 9:08 AM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Tuesday, June 3, 2014 4:20 AM
Points: 69, Visits: 363
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

Post #1339263
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse