From dedecb492e640d81c974261d12329b89a6839abd Mon Sep 17 00:00:00 2001 From: vicent Date: Wed, 23 Feb 2022 12:58:01 +0100 Subject: [PATCH] sql modify --- .../00-vendedores_updatePortfolio.sql | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 db/changes/14030-ashWednesday/00-vendedores_updatePortfolio.sql diff --git a/db/changes/14030-ashWednesday/00-vendedores_updatePortfolio.sql b/db/changes/14030-ashWednesday/00-vendedores_updatePortfolio.sql new file mode 100644 index 000000000..f4c8fd7e8 --- /dev/null +++ b/db/changes/14030-ashWednesday/00-vendedores_updatePortfolio.sql @@ -0,0 +1,30 @@ +DROP PROCEDURE IF EXISTS bs.vendedores_updatePortfolio; + +DELIMITER $$ +$$ +CREATE DEFINER=`root`@`localhost` PROCEDURE `bs`.`vendedores_updatePortfolio`() +BEGIN + /** + * Actualiza el campo portfolioWeight que indica el peso de la cartera del comercial + * + */ + DECLARE vStarted DATE DEFAULT DATE_FORMAT(DATE_ADD(CURDATE(), INTERVAL -17 MONTH), '%Y-%m-01'); + + DELETE v + FROM vendedores v + JOIN vn.`time` t ON t.`year` = v.año AND t.`month` = v.mes + WHERE t.dated BETWEEN vStarted AND CURDATE(); + + INSERT INTO vendedores(Id_Trabajador, año, mes, portfolioWeight) + SELECT c.lastSalesPersonFk + , t.`year` + , t.`month` + , SUM(v.importe) + FROM vn.`time` t + STRAIGHT_JOIN bs.ventas v ON t.dated = v.fecha + JOIN vn.`client` c ON c.id = v.Id_Cliente + WHERE c.lastSalesPersonFk IS NOT NULL + AND v.fecha BETWEEN vStarted AND CURDATE() + GROUP BY c.lastSalesPersonFk, t.`month`, t.`year`; +END$$ +DELIMITER ;