33 lines
1.3 KiB
SQL
33 lines
1.3 KiB
SQL
DELIMITER $$
|
|
CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `bi`.`comparativa_add`()
|
|
BEGIN
|
|
DECLARE lastCOMP INT; # Se trata de una variable para almacenar el ultimo valor del Periodo
|
|
DECLARE vMaxPeriod INT;
|
|
DECLARE vMaxWeek INT;
|
|
|
|
SELECT t.period, t.`week` INTO vMaxPeriod, vMaxWeek
|
|
FROM vn.`time` t
|
|
WHERE t.dated = util.VN_CURDATE();
|
|
|
|
SELECT MAX(Periodo) INTO lastCOMP FROM vn2008.Comparativa;
|
|
-- Fijaremos las ventas con más de un mes de antiguedad en la tabla Comparativa
|
|
|
|
IF lastCOMP < vMaxPeriod - 3 AND vMaxWeek > 3 THEN
|
|
|
|
REPLACE vn2008.Comparativa(Periodo, Id_Article, warehouse_id, Cantidad,price)
|
|
SELECT tm.period as Periodo, m.Id_Article, t.warehouseFk, sum(m.Cantidad), sum(v.importe)
|
|
FROM bs.ventas v
|
|
JOIN vn2008.time tm ON tm.date = v.fecha
|
|
JOIN vn2008.Movimientos m ON m.Id_Movimiento = v.Id_Movimiento
|
|
JOIN vn2008.Tipos tp ON tp.tipo_id = v.tipo_id
|
|
JOIN vn2008.reinos r ON r.id = tp.reino_id
|
|
JOIN vn.ticket t ON t.id = m.Id_Ticket
|
|
WHERE tm.period BETWEEN lastCOMP AND vMaxPeriod - 3
|
|
AND t.clientFk NOT IN(400,200)
|
|
AND t.warehouseFk NOT IN (0,13)
|
|
GROUP BY m.Id_Article, Periodo, t.warehouseFk;
|
|
|
|
END IF;
|
|
END$$
|
|
DELIMITER ;
|