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.