DELIMITER $$ CREATE OR REPLACE DEFINER=`vn`@`localhost` PROCEDURE `vn`.`zone_getClosed`() proc:BEGIN /** * Devuelve una tabla con las zonas cerradas para hoy * * @table tmp.closedZones(zoneFk, warehouseFk); */ DROP TEMPORARY TABLE IF EXISTS tmp.closedZones; CREATE TEMPORARY TABLE tmp.closedZones ( `zoneFk` int(11) NOT NULL, `warehouseFk` smallint(6) unsigned NOT NULL, PRIMARY KEY `zoneFk` (`zoneFk`,`warehouseFk`), KEY `warehouseFk` (`warehouseFk`)) ENGINE=MEMORY DEFAULT CHARSET=utf8; INSERT INTO tmp.closedZones(zoneFk, warehouseFk) SELECT zoneFk, warehouseFk FROM ( SELECT t.zoneFk, t.warehouseFk, count(DISTINCT t.id) numTickets, pc.minTicketsToCloseZone FROM vn.ticket t JOIN vn.expedition e ON e.ticketFk = t.id JOIN vn.`zone`z on z.id = t.zoneFk JOIN vn.agencyMode am ON am.id = z.agencyModeFk JOIN vn.agency a ON a.id = am.agencyFk JOIN vn.productionConfig pc WHERE a.isOwn AND pc.isZoneClosedByExpeditionActivated AND e.created >= util.VN_CURDATE() GROUP BY t.zoneFk, t.warehouseFk HAVING numTickets >= minTicketsToCloseZone) sub; END$$ DELIMITER ;