I would like to join on a many to one relationship, but pick one of the records to join based a condition, in this case the maximum date. Here is a simplified schema;
parent
---------
id | name
---------
1 | Bob
2 | Mary
3 | Pat
child
----------------------------------
id | parent_id | name | birthdate
----------------------------------
11 | 1 | Anne | 2014-01-01
12 | 2 | Jane | 2014-01-02
13 | 3 | John | 2016-06-01
14 | 1 | Mark | 2016-08-01
15 | 2 | Jack | 2016-01-01
16 | 2 | Jill | 2016-01-01
A parent can have zero or more children. A child's birthdate can be in the future (a pregnancy). Two children from the same parent can have the same birthdate (twins).
I would like to get the parent record and their youngest child. In the case of youngest twins, I don't care which child is chosen. So in the this case, Bob has two children, their youngest is Mark who is due to be born on August 8. Mary has 3 children. Her two youngest are twins, Jack and Jill. Pat has no children. So I would like to get;
id | name | child | birthdate
-----------------------------
1 | Bob | Mark | 2016-08-01
2 | Mary | Jack | 2016-01-01
3 | Pat | null | null
Is it possible to do this in a single query?