Hi Phiali, thanks for your fast response!
That’s an excellent suggestion regarding the excluded dates… I did not understand what the intention of those fields were until you had explained! The documentation is very vague on the subject…
Here’s my attempt at replicating your SQL statement (“Except” doesn’t work on MySQL)
SELECT `service_id`,monday,tuesday,wednesday,thursday,friday,saturday,sunday, `remove`, `add` FROM (
SELECT `service_id`,monday,tuesday,wednesday,thursday,friday,saturday,sunday, SUM(`add`) "add", SUM(`remove`) "remove" FROM (
SELECT `calendar`.`service_id`, 1 "add", 0 "remove", monday,tuesday,wednesday,thursday,friday,saturday,sunday FROM calendar
LEFT JOIN `calendar_dates` ON `calendar`.`service_id` = `calendar_dates`.`service_id`
WHERE start_date <= NOW() AND end_date >= NOW() AND exception_type = 2
UNION
SELECT `calendar`.`service_id`, 0 "add", 1 "remove", monday,tuesday,wednesday,thursday,friday,saturday,sunday FROM calendar
LEFT JOIN `calendar_dates` ON `calendar`.`service_id` = `calendar_dates`.`service_id`
WHERE start_date <= NOW() AND end_date >= NOW() AND exception_type = 1
) serviceDescriptors GROUP BY `service_id`
) filteredServices /*WHERE `remove` = 0 AND `add` = 1*/;
I’ve implemented this SQL into my query, but it doesn’t seem to have augmented the results much.
I’ve had to suddenly drop this project - we’re handing development over to a third party agency. Since my priorities are shifted I can’t spend time to verify if your solution will work. Hopefully it’ll help the next person! I’ll try come back to this to make a more fuller answer but won’t guaruntee. Thanks again Phiali!