with bookingdetails as (
select booking_master.booking_datetime as newdatetime,
booking_master.ETA as neweta
from booking_master where booking_ID='143'
2016-10-02 03:00:00.000 2016-10-05 03:00:00.000
with comparetrucklog as (
select truck_log.truck_id as bookedtruck,
truck_log.ETA as eta
where (select newdatetime from bookingdetails) between truck_log.pickupdate and truck_log.ETA
or (select neweta from bookingdetails) between truck_log.pickupdate and truck_log.ETA
why simple max() will work right?
select bookedtruck, max(eta) from bookingmaster group by bookedtruck