I need to join two tables that are described below:
Table1:
ID Date Info1
1 1/29/2011 i10
1 1/30/2011 i11
Table2:
ID Date Info2
1 1/31/2011 i2
I would like to left join the records in Table 2 identified by ID, Month, Year to that in Table 1 identified by the same ID, Month, Year but use the last available record date as the joining record. So for example, in the data above I would join the record in Table 2 to the second record in Table 1 because they match in ID, Month, Year and record 2 of Table 1 has the greatest available day for that (ID, Month, Year) combination. The correct result is:
ID Date Info1 Info2
1 1/30/2011 i11 i2
The SQL code I am coming up with so far is pretty convoluted. Please suggest something. I am using MySQL.
Solve that first, with a derived table. Assuming that
ID, Dateis unique, then you can easily group by ID and take the MAX date.