CREATE OR REPLACE DEFINER=`vn`@`localhost` SQL SECURITY DEFINER VIEW `vn`.`expeditionRoute_Monitor` AS SELECT `r`.`id` AS `routeFk`, COUNT(DISTINCT IF(`e`.`id` IS NULL, `t`.`id`, NULL)) AS `tickets`, COUNT(DISTINCT `e`.`id`) AS `expeditions`, COUNT(DISTINCT `es`.`id`) AS `scanned`, max(`e`.`created`) AS `lastPacked`, `r`.`dated` AS `created` FROM ( ( ( ( ( `vn`.`route` `r` LEFT JOIN `vn`.`routesMonitor` `rm` ON(`r`.`id` = `rm`.`routeFk`) ) LEFT JOIN `vn`.`roadmapStop` `rs` ON(`rs`.`id` = `rm`.`roadmapStopFk`) ) JOIN `vn`.`ticket` `t` ON(`t`.`routeFk` = `r`.`id`) ) LEFT JOIN `vn`.`expedition` `e` ON(`e`.`ticketFk` = `t`.`id`) ) LEFT JOIN `vn`.`expeditionScan` `es` ON(`es`.`expeditionFk` = `e`.`id`) ) WHERE `r`.`dated` >= `util`.`yesterday`() GROUP BY `r`.`id`