Mi riporta indietro, ma questo è ciò che vuoi mostrare TUTTO a noleggio nel periodo, compreso il non reso
select *
from MyTable
where on_hire < @EndDate
and (off_hire >= @StartDate or off_hire is null)
Per il follow-up, numero totale di giorni per ogni strumento
with CTE as
(
select *
from MyTable
where on_hire < @EndDate
and (off_hire >= @StartDate or off_hire is null)
)
select Tool,
sum(datediff(dd,
case
when off_hire > @EndDate then @EndDate
when off_hire is null then @EndDate
else off_hire
end,
case
when on_hire < @StartDate then @StartDate
else on_hire
end)) as DaysOnHire
from CTE
froup by Tool