I have a system which inserts records in to two tables (Oracle db but this is a general SQL question I guess):
Person table and Person_Record table (one to one relationship)
The Person table has a date of when it was inserted, which is important later on.
What to query is: A join between the two tables but only give me the most recent data for a given person. For example:
Person Table
ID| Name | Date
--------------------------
1 | A | 2012-05-01
2 | A | 2012-05-02
3 | B | 2012-05-04
Person Record Table
ID| Person_Id | Data
--------------------------
1 | 1 | my data 1
2 | 2 | my data 2
3 | 3 | my data 3
If I do:
SELECT pr.record_id, p.person_name,
FROM PERSON p
INNER JOIN PERSON_RECORD pr
ON (p.person_id = pr.person_id)
I'll get 2 rows back for the person with the name 'A' and 1 record back for 'B' but I don't care about the first row for person 'A' (from 2012-05-01). I have tried using distinct to get rid of duplicates (on the name) but I need a guarantee that I'm getting the most recent date for that particular name. Feel like I'm missing something obvious but I can't get it to click.