Try this:update table1 set table1.year=table2.maxYear ,table1.months=table2.maxmonths from table1 inner join (select ID ,max(Year) as maxYear ,max(months) as maxmonths from table2 group by ID ) as table2 on table2.ID=table1.ID and (table2.maxYear<>isnull(table1.year,0) or table2.maxmonths<>isnull(table1.months,0) )
In case you have records in table2 which are not present in table1, run this:insert into table1 (ID,year,months)select ID ,max(Year) ,max(months) from table2 where not exists(select * from table1 where table1.ID=table2.ID ) group by ID