CREATE OR REPLACE DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `vn`.`expeditionTruck_Control_Detail_Pallet` AS SELECT `e`.`truckFk` AS `id`, `e`.`eta` AS `eta`, `e`.`description` AS `destino`, `e`.`palletFk` AS `pallet`, `e`.`routeFk` AS `route`, COUNT(DISTINCT `e`.`scanFk`) AS `scans`, `et`.`description` AS `destinos`, sum(`e`.`truckFk` <> `e`.`expeditionTruckFk`) AS `fallos`, `e`.`expeditionTruckFk` AS `expeditionTruckFk`, max(`e`.`lastPacked`) AS `lastPacked` FROM ( `vn`.`expeditionCommon` `e` LEFT JOIN `vn`.`expeditionTruck` `et` ON(`et`.`id` = `e`.`expeditionTruckFk`) ) GROUP BY `e`.`truckFk`, `e`.`palletFk`, `e`.`routeFk` ORDER BY sum(`e`.`truckFk` <> `e`.`expeditionTruckFk`) DESC, `e`.`palletFk`