Brengt me terug, maar dit is wat je ALLES te huur wilt laten zien in de periode, inclusief niet-geretourneerde artikelen
select *
from MyTable
where on_hire < @EndDate
and (off_hire >= @StartDate or off_hire is null)
Voor de follow-up, totaal aantal dagen voor elke tool
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