Tar mig tillbaka, men det här är vad du vill visa ALLT på hyra under perioden, inklusive ej returnerade varor
select *
from MyTable
where on_hire < @EndDate
and (off_hire >= @StartDate or off_hire is null)
För uppföljningen, totalt antal dagar för varje verktyg
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