I have a task to roll up a test sample.
id,age,parentid
1,56,0
2,23,1
3,18,1
4,76,0
5,34,4
6,51,4
7,43,4
8,43,0
9,19,8
10,17,8
11,16,8
Result
parentid,youngest,eldest
1,3,2
4,5,6
8,11,9
I have the script to set the parent ID, but the crating the results I am stuck on. Can anyone give pointers or help?
This is how I set the parent ID
SELECT C.aid as ID,c.aage as age, min(D.parentid) as parentid
INTO #Test2_Output
FROM Scott_Work.dbo.Test2 as D
JOIN
--This section pulls of the linked ID's into a group(C) based on what matched (parentid)
(SELECT A.ID as aid, B.ID as bid,a.age as aage FROM Scott_Work.dbo.Test2 as A JOIN Scott_Work.dbo.Test2 as B ON A.parentid = B.parentid) AS C
ON C.bid = D.ID
group by C.aid,c.aage order by 1