DELIMITER $$ CREATE OR REPLACE DEFINER=`vn-admin`@`localhost` PROCEDURE `vn`.`ticketBoxesView`(IN vTicketFk INT) BEGIN SELECT s.id, s.itemFk, s.concept, floor(s.quantity / b.packing) as Cajas, b.packing, s.isPicked, i.size FROM ticket t JOIN sale s ON s.ticketFk = t.id JOIN item i ON i.id = s.itemFk JOIN cache.last_buy lb on lb.warehouse_id = t.warehouseFk AND lb.item_id = s.itemFk JOIN buy b on b.id = lb.buy_id JOIN packaging p on p.id = b.packagingFk WHERE s.quantity >= b.packing AND t.id = vTicketFk AND p.isBox GROUP BY s.itemFk; END$$ DELIMITER ;