CREATE OR REPLACE DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `vn`.`expeditionTruck_Control` AS SELECT `e`.`truckFk` AS `id`, `e`.`eta` AS `ETD`, `e`.`description` AS `description`, COUNT( DISTINCT IF(`e`.`expeditionFk` IS NULL, `e`.`ticketFk`, NULL) ) AS `ticketsSinBultos`, COUNT(DISTINCT `e`.`palletFk`) AS `pallets`, COUNT(DISTINCT `e`.`routeFk`) AS `routes`, COUNT(DISTINCT `e`.`scanFk`) AS `scans`, COUNT(DISTINCT `e`.`expeditionFk`) AS `expeditions`, sum(`e`.`truckFk` <> `e`.`expeditionTruckFk`) AS `fallos`, max(`e`.`lastPacked`) AS `lastPacked` FROM `vn`.`expeditionCommon` `e` GROUP BY `e`.`truckFk` ORDER BY sum(`e`.`truckFk` <> `e`.`expeditionTruckFk`) DESC, `e`.`eta`