select * from #t1 across apply (select b.prodid,max(daytime)dt from ProductRateMarginLog b where b.daytime<a.daytime and a.prodid=b.prodid group by b.prodid) b