From ed0f942a5780ee083036642290d2e2bb0b1bed67 Mon Sep 17 00:00:00 2001 From: ivanm Date: Tue, 30 Jan 2024 09:45:37 +0100 Subject: [PATCH] refs #6444 create vn.intrastat_estimateNet.sql --- .../vn/functions/intrastat_estimateNet.sql | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 db/routines/vn/functions/intrastat_estimateNet.sql diff --git a/db/routines/vn/functions/intrastat_estimateNet.sql b/db/routines/vn/functions/intrastat_estimateNet.sql new file mode 100644 index 000000000..4d66e30de --- /dev/null +++ b/db/routines/vn/functions/intrastat_estimateNet.sql @@ -0,0 +1,19 @@ +DELIMITER $$ +CREATE OR REPLACE DEFINER=`root`@`localhost` FUNCTION `vn`.`intrastat_estimateNet`(intINSTRASTAT INTEGER,intUNIDADES INTEGER) + RETURNS double + DETERMINISTIC +BEGIN + + DECLARE n DOUBLE; + + SELECT ROUND(intUNIDADES / (SUM(MEDIA) / COUNT(media)), 2) INTO n FROM + (SELECT *, unidades / neto MEDIA + FROM vn2008.intrastat_data + WHERE intrastat_id = intINSTRASTAT AND neto + AND unidades > 0 + ORDER BY odbc_date DESC + LIMIT 20) t; + RETURN n/2; + +END$$ +DELIMITER ; \ No newline at end of file