Me lleva de vuelta, pero esto es lo que quiere mostrar TODO en alquiler en el período, incluidos los elementos no devueltos
select *
from MyTable
where on_hire < @EndDate
and (off_hire >= @StartDate or off_hire is null)
Para el seguimiento, número total de días para cada herramienta
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