1

How can I get only the eldest son of each parent when I have N parents each with N children? A parent can have multiple children, but each child can have only one parent.

For example:

Table 1 (Parents)
- John
- Maria
- Steve
Table 2 (Children)
- Bob, 8
- Sandra, 10
- Steve, 20
- Mario, 25
- Lucas, 50
- Sam, 2
- Ricardo, 7
Table 3 (Parents_Children)
- John | Bob, 8
- John | Sandra, 10
- John | Steve, 20
- Maria | Mario, 25
- Maria | Lucas 50
- Steve | Sam 2
- Steve | Ricardo, 7

I want to make an appointment that returns 100% of my parents, but only the oldest child.

The result would have to be:

- John | Steve, 20
- Maria | Lucas 50
- Steve | Ricardo 7

I get the result when I search with just one parent:

select top 1 
    p.Name, 
    c.Name, 
    c.age 
from Parents as p
join Parent_Children as pc on pc.ParentId = p.Id
join Children as c on c.Id = pc.ChildrenId
where 
    p.Name = 'John'
order 
    by c.age desc

But I wanted with a single query to return this result to all parents.

2
  • 4
    Please ensure your entire post (including title) is in English. Thank youm
    – Thom A
    Commented Jan 11, 2020 at 23:04
  • @DiegoRodriguesdeFaria . . . In the first sentence, I think you mean "child", not "son". In English, "child" is gender-neutral. "Son" and "daughter" are gender-specific, for "male child" and "female child" respectively. Commented Jan 12, 2020 at 13:56

4 Answers 4

2

Your sample data has no gender information, so I am interpreting "oldest son" as "oldest child".

You can use window functions:

select c.*
from (select pc.parentid, c.*,
             row_number() over (partition by pc.parentid order by c.age desc) as seqnum
      from Parent_Children pc join
           Children c
           on c.Id = pc.ChildrenId
     ) c
where seqnum = 1;

In the event of twins (or other multiple births), this returns one arbitrarily. To return all of them, use rank() instead of row_number().

1
  • 1
    @DaleK . . . Not in my mental image. Thank you. Commented Jan 12, 2020 at 19:06
0
;with cte as (
    select p.parent_name, c.child_name, c.age
        , rank() over (partition by parent_name order by c.age desc) as Rank
    from Parents p
    inner join Parent_Children pc on pc.pid = p.pid
    inner join Children c on c.cid = pc.cid
)
select parent_name, child_name, age
from cte
where Rank = 1;

Rank() or Dense_Rank(), both functions can be used here to get the desired result, but ideally Children table should have an extra column as Parent_Id with foreign key as PId from Parent table.

This will reduce the need of having third table and will save one join.

1
  • How does your answer differ from @satishcse's?
    – Dale K
    Commented Jan 12, 2020 at 4:03
0

Try the following:

declare @parents table (pid int, parent_name varchar(100))
insert into @parents select 1, 'John'
insert into @parents select 2, 'Maria'
insert into @parents select 3, 'Steve'

--select * from @parents

declare @children table (cid int, child_name varchar(100), age int)
insert into @children select 1, 'Bob', 8
insert into @children select 2, 'Sandra', 10
insert into @children select 3, 'Steve', 20
insert into @children select 4, 'Mario', 25
insert into @children select 5, 'Lucas', 50
insert into @children select 6, 'Sam', 2
insert into @children select 7, 'Ricardo', 7

--select * from @children

declare @parent_children table (pid varchar(100), cid varchar(100), age int)
insert into @parent_children select 1 , 1, 8
insert into @parent_children select 1 , 2, 10
insert into @parent_children select 1 , 3, 20
insert into @parent_children select 2, 4, 25
insert into @parent_children select 2, 5, 50
insert into @parent_children select 3, 6, 2
insert into @parent_children select 3, 7, 7

--select * from @parent_children

;with cte
as
(
    select p.parent_name, 
        c.child_name, 
        c.age,
        rank() over (partition by parent_name order by c.age desc) rnk
    from @parents as p
    join @parent_children as pc on pc.pid = p.pid
    join @children as c on c.cid = pc.cid
)
select parent_name, child_name, age
from cte
where rnk = 1
0

Here is a way to get that:

with cte as
(     
    select 'John' as Parent, 'Bob, 8' as Children
    union select 'John', 'Sandra, 10'
    union select 'John', 'Steve, 20'
    union select 'Maria', 'Mario, 25'
    union select 'Maria', 'Lucas, 50'
    union select 'Steve', 'Sam, 2'
    union select 'Steve', 'Ricardo, 7'
),
-- add age as column
cte2 as 
(
    select Parent, Children, cast(right(Children, len(Children) - CHARINDEX(',', Children , 1)) as int) as Age
    from cte 
),
-- cte2 is to get the max age per Parent
cte3 as
(
    select Parent, max(Age) as Age
    from cte2 a
    group by Parent
)
select a.Parent, Children
from cte2 a
inner join cte3 b on a.Parent = b.Parent and a.Age = b.Age   

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.