salix/db/routines/vn/views/expeditionRoute_Monitor.sql

28 lines
845 B
SQL

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`.`expeditionTruck` `et` ON(`et`.`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`