select * from table1 as a left join table2 as b on a.ID = b.IDwhere exists(select table1 from (select ID ,max(DateTime) as Timefrom table1 group by ID)as n where n.ID= b.ID and n.Time= a.DateTime)