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