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

28 lines
835 B
MySQL
Raw Permalink Normal View History

2024-08-20 08:06:10 +00:00
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`.`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`.`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`.`created` >= `util`.`yesterday`()
GROUP BY `r`.`id`