DELIMITER $$ CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `vn2008`.`item_express`(IN intWH INT, IN datFEC DATE) BEGIN DECLARE datINV DATE; SELECT FechaInventario INTO datINV FROM tblContadores LIMIT 1; SELECT sub1.Id_Article, Visible, Tarifa2 FROM (SELECT Id_Article, SUM(Cantidad) as Visible FROM Compres C JOIN Entradas E USING(Id_Entrada) JOIN travel T ON T.id = E.travel_id WHERE shipment >= datINV AND landing <= datFEC AND Redada = 0 AND warehouse_id = intWH GROUP BY Id_Article) sub1 JOIN ( SELECT * FROM ( SELECT Id_Article, Tarifa2 FROM Compres C JOIN Entradas E USING(Id_Entrada) JOIN travel T ON T.id = E.travel_id WHERE shipment >= datINV AND landing <= datFEC AND Redada = 0 AND Novincular = 0 AND warehouse_id = intWH ORDER BY landing DESC LIMIT 10000000000000000000) sub GROUP BY Id_Article) sub2 USING(Id_Article); END$$ DELIMITER ;