From 6fb94188f5fcc9c18e78855648cd63b90b78ec42 Mon Sep 17 00:00:00 2001 From: Bernat Date: Fri, 29 Mar 2019 13:15:55 +0100 Subject: [PATCH] cau 10246 add new procedure --- .../only-local/00-routeGuessPriority.sql | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 db/install/changes/only-local/00-routeGuessPriority.sql diff --git a/db/install/changes/only-local/00-routeGuessPriority.sql b/db/install/changes/only-local/00-routeGuessPriority.sql new file mode 100644 index 000000000..bf8ddcecd --- /dev/null +++ b/db/install/changes/only-local/00-routeGuessPriority.sql @@ -0,0 +1,24 @@ + +DROP procedure IF EXISTS `vn`.`routeGuessPriority`; + +DELIMITER $$ +CREATE DEFINER=`root`@`%` PROCEDURE `vn`.`routeGuessPriority`(IN vRuta INT) +BEGIN +/* Usa los valores del ultimo año para adivinar el orden de los tickets en la ruta + * vRuta id ruta +*/ +UPDATE ticket mt + JOIN ( + SELECT tt.addressFk, ROUND(IFNULL(AVG(t.priority),0),0) AS priority + FROM ticket t + JOIN ticket tt ON tt.addressFk = t.addressFk + WHERE t.shipped > TIMESTAMPADD(YEAR,-1,CURDATE()) + AND tt.routeFk = vRuta + GROUP BY addressFk + ) sub ON sub.addressFk = mt.addressFk + SET mt.priority = sub.priority + WHERE mt.routeFk = vRuta; +END$$ + +DELIMITER ; +