You can use apply
:
select a.*, b.*
from a cross apply
(select top (1) b.*
from b
where b.code = a.code and b.time < getdate()
order by b.time desc
) b;
This assumes that time
is really a datetime
. If you just want to compare times, then use convert(time, getdate())
.
2
solved Left join combining GETDATE()