DELIMITER $$ CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `vn2008`.`historico_multiple`(IN vItemFk INT) BEGIN DECLARE vDateInventory DATETIME; SELECT inventoried INTO vDateInventory FROM vn.config; SET @a = 0; DROP TEMPORARY TABLE IF EXISTS hm1; CREATE TEMPORARY TABLE hm1 SELECT DATE(Fecha) as Fecha, Entrada, Salida, OK, Referencia, Historia.id, wh, `name` as wh_name FROM ( SELECT TR.landing as Fecha, C.Cantidad as Entrada, NULL as Salida, IF(warehouse_id = 44, 1, warehouse_id) as wh, (TR.received != FALSE) as OK, E.Referencia as Referencia, E.Id_Entrada as id FROM Compres C INNER JOIN Entradas E USING (Id_Entrada) INNER JOIN travel TR ON TR.id = E.travel_id WHERE TR.landing >= vDateInventory AND C.Id_Article = vItemFk AND E.Redada = 0 AND C.Cantidad <> 0 UNION ALL SELECT TR.shipment as Fecha, NULL as Entrada, C.Cantidad as Salida, warehouse_id_out as wh, TR.delivered as OK, E.Referencia as Referencia, E.Id_Entrada as id FROM Compres C INNER JOIN Entradas E USING (Id_Entrada) INNER JOIN travel TR ON TR.id = E.travel_id WHERE TR.shipment >= vDateInventory AND C.Id_Article = vItemFk AND E.Redada = 0 AND C.Cantidad <> 0 UNION ALL SELECT t.shipped as Fecha, NULL as Entrada, M.Cantidad as Salida, t.warehouseFk as wh, (M.OK <> 0 OR t.isLabeled <> 0 OR t.refFk IS NOT NULL) as OK, t.refFk as Referencia, t.id as id FROM Movimientos M INNER JOIN vn.ticket t ON t.id = M.Id_Ticket WHERE t.shipped >= vDateInventory AND M.Id_Article = vItemFk ) AS Historia INNER JOIN vn.warehouse ON warehouse.id = Historia.wh ORDER BY Fecha, Entrada DESC, OK DESC; DROP TEMPORARY TABLE IF EXISTS hm2; DROP TEMPORARY TABLE IF EXISTS hm3; DROP TEMPORARY TABLE IF EXISTS hm4; DROP TEMPORARY TABLE IF EXISTS hm5; DROP TEMPORARY TABLE IF EXISTS hm6; DROP TEMPORARY TABLE IF EXISTS hm7; DROP TEMPORARY TABLE IF EXISTS hm8; CREATE TEMPORARY TABLE hm2 SELECT * FROM hm1 WHERE wh = 19; CREATE TEMPORARY TABLE hm3 SELECT * FROM hm1 WHERE wh = 7; CREATE TEMPORARY TABLE hm4 SELECT * FROM hm1 WHERE wh = 60; CREATE TEMPORARY TABLE hm5 SELECT * FROM hm1 WHERE wh = 5; CREATE TEMPORARY TABLE hm6 SELECT * FROM hm1 WHERE wh = 17; CREATE TEMPORARY TABLE hm7 SELECT * FROM hm1 WHERE wh = 37; CREATE TEMPORARY TABLE hm8 SELECT * FROM hm1 WHERE wh = 55; SELECT * FROM ( SELECT Fecha, Entrada as BOGEntrada, Salida as BOGSalida, OK as BOGOK, Referencia as BOGReferencia, id as BOGid, NULL AS VNHEntrada, NULL AS VNHSalida, NULL AS VNHOK, NULL AS VNHReferencia, NULL AS VNHid, NULL AS ALGEntrada, NULL AS ALGSalida, NULL AS ALGOK, NULL AS ALGReferencia, NULL AS ALGid, NULL AS MADEntrada, NULL AS MADSalida, NULL AS MADOK, NULL AS MADReferencia, NULL AS MADid, NULL AS MCFEntrada, NULL AS MCFSalida, NULL AS MCFOK, NULL AS MCFReferencia, NULL AS MCFid, NULL AS VILEntrada, NULL AS VILSalida, NULL AS VILOK, NULL AS VILReferencia, NULL AS VILid, NULL AS BAREntrada, NULL AS BARSalida, NULL AS BAROK, NULL AS BARReferencia, NULL AS BARid FROM hm2 UNION ALL SELECT Fecha , NULL, NULL, NULL, NULL, NULL ,Entrada, Salida, OK, Referencia, id , NULL, NULL, NULL, NULL, NULL , NULL, NULL, NULL, NULL, NULL , NULL, NULL, NULL, NULL, NULL , NULL, NULL, NULL, NULL, NULL , NULL, NULL, NULL, NULL, NULL FROM hm3 UNION ALL SELECT Fecha , NULL, NULL, NULL, NULL, NULL , NULL, NULL, NULL, NULL, NULL , Entrada, Salida, OK, Referencia, id , NULL, NULL, NULL, NULL, NULL , NULL, NULL, NULL, NULL, NULL , NULL, NULL, NULL, NULL, NULL , NULL, NULL, NULL, NULL, NULL FROM hm4 UNION ALL SELECT Fecha , NULL, NULL, NULL, NULL, NULL , NULL, NULL, NULL, NULL, NULL , NULL, NULL, NULL, NULL, NULL , Entrada, Salida, OK, Referencia, id , NULL, NULL, NULL, NULL, NULL , NULL, NULL, NULL, NULL, NULL , NULL, NULL, NULL, NULL, NULL FROM hm5 UNION ALL SELECT Fecha , NULL, NULL, NULL, NULL, NULL , NULL, NULL, NULL, NULL, NULL , NULL, NULL, NULL, NULL, NULL , NULL, NULL, NULL, NULL, NULL , Entrada, Salida, OK, Referencia, id , NULL, NULL, NULL, NULL, NULL , NULL, NULL, NULL, NULL, NULL FROM hm6 UNION ALL SELECT Fecha , NULL, NULL, NULL, NULL, NULL , NULL, NULL, NULL, NULL, NULL , NULL, NULL, NULL, NULL, NULL , NULL, NULL, NULL, NULL, NULL , NULL, NULL, NULL, NULL, NULL , Entrada, Salida, OK, Referencia, id , NULL, NULL, NULL, NULL, NULL FROM hm7 UNION ALL SELECT Fecha , NULL, NULL, NULL, NULL, NULL , NULL, NULL, NULL, NULL, NULL , NULL, NULL, NULL, NULL, NULL , NULL, NULL, NULL, NULL, NULL , NULL, NULL, NULL, NULL, NULL , NULL, NULL, NULL, NULL, NULL , Entrada, Salida, OK, Referencia, id FROM hm8 ) sub ORDER BY Fecha, BOGEntrada IS NULL, VNHEntrada IS NULL, ALGEntrada IS NULL, MADEntrada IS NULL, MCFEntrada IS NULL, VILEntrada IS NULL, BAREntrada IS NULL; END$$ DELIMITER ;