CREATE OR REPLACE DEFINER=`root`@`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`.`created` 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`.`expeditionTruckFk`) ) 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`.`created` >= `util`.`yesterday`() GROUP BY `r`.`id`