Tager mig tilbage, men det er det du vil vise ALT på leje i perioden, inklusive ikke returnerede varer
select *
from MyTable
where on_hire < @EndDate
and (off_hire >= @StartDate or off_hire is null)
Til opfølgningen, det samlede antal dage for hvert værktøj
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