I'm getting extra days when querying for room occupancy. I think this is because it still does SUM(DATEDIFF(...) +1)
even on days with two reservations. So for example in february it returns 31 days for a room.
How can i add one day except for days with two reservations, then it only has to +1 it for one of the two.
SELECT
room_id,
SUM(DATEDIFF(
LEAST(end_time, '2019-12-31 23:59:59'),
GREATEST(start_time, '2019-12-01 00:00:00')
) + 1) as days
FROM reservations
WHERE
(start_time >= '2019-12-01 00:00:00' AND start_time < '2020-01-01 00:00:00')
OR
(end_time >= '2019-12-01 00:00:00' AND end_time < '2020-01-01 00:00:00')
GROUP BY room_id
Here's a fiddle that represents the unneeded extra day, https://www.db-fiddle.com/f/ta53CprLisnRXh9A8wVwZv/1 it returns 7 days but the room is reserved for 5