I have two tables in my sql database:
Columns in table 1:
entity ID, contact person, contact ID, created date
Columns in Table 2:
contact ID entity ID modified date contact person
1 2 10/12/13 MR.A
1 2 11/12/13 MR.B
4 16 17/12/13 MR.C
4 16 19/12/13 MR.D
I want to insert records from table 2 to table 1 where modified date is maximum for the same contact ID.
The output should be like:
Table 1:
entity ID contact person contact ID created date
2 MR.B 1 11/12/13
16 MR.D 4 19/12/13
Please suggest how to write such a SQL query. I have used some queries with max and have but didn't get my answer, guess that's not correctly placed ?
Thanks in advance
It looks like you don't have a primary key on Table2. If you did then this would be much easier as you could use that in the join. However as you don't then this sql does the trick:
Sql fiddle can be found here