From f6db824d75d747b6a1f94c4aacec5bae5ac4d5e5 Mon Sep 17 00:00:00 2001
From: "Campos Cordobes, Sergio" <sergio.campos@tecnalia.com>
Date: Mon, 24 Apr 2023 08:35:33 +0200
Subject: [PATCH] Update ddbb scripts/recommender_plans.sql

---
 ddbb scripts/recommender_plans.sql | 100 +++++++++++++++++++++++++++++
 1 file changed, 100 insertions(+)
 create mode 100644 ddbb scripts/recommender_plans.sql

diff --git a/ddbb scripts/recommender_plans.sql b/ddbb scripts/recommender_plans.sql
new file mode 100644
index 0000000..05f2fd1
--- /dev/null
+++ b/ddbb scripts/recommender_plans.sql	
@@ -0,0 +1,100 @@
+-- Created on: 16/01/2023
+-- @author: Andoni Aranguren Ubierna
+-- Adaptations: 02/2023 @author: Sergio Campos
+
+drop procedure `recommender_popularity_detailed`;
+DELIMITER $$
+CREATE DEFINER=`root`@`localhost` PROCEDURE `recommender_popularity_detailed`(IN kpi INT, IN action INT)
+BEGIN
+        IF ((kpi is null) and (action is NULL))  THEN
+                SELECT t_p_list.plan_id,p.action_id, p.action_name, p.lat, p.lon,
+                -- This lines are so we can get the list of kpis vvvvvvvvvv
+                        GROUP_CONCAT(distinct t_p_list.plan_id) as plan_id_list,
+                        GROUP_CONCAT(distinct t_p_list.kpi_id) as kpi_id_list,
+                        GROUP_CONCAT(distinct t_p_list.kpi_name) as kpi_name_list,
+            		GROUP_CONCAT(t_p_list.absolute) as kpi_absolute_list,
+            		GROUP_CONCAT(t_p_list.relative) as kpi_relative_list,
+            		GROUP_CONCAT(t_p_list.geographical) as kpi_geographical_list,
+                -- This lines are so we can get the list of kpis ^^^^^^^^^^
+                p.popularity as popularity, p.category as category
+                FROM action p
+        -- This lines are so we can get the list of kpis vvvvvvvvvv
+                        INNER JOIN chosen_action t_list ON t_list.action_id = p.action_id
+                        INNER JOIN kpi_action_plan_detail t_p_list ON t_list.plan_id = t_p_list.plan_id
+                        INNER JOIN action a ON a.action_id = t_p_list.action_id
+        -- This lines are so we can get the list of kpis ^^^^^^^^^^
+        	GROUP BY p.action_id
+                ORDER BY popularity DESC
+                LIMIT 100;
+
+	ELSEIF (not(kpi is null))  THEN
+                SELECT t_p_list.plan_id,p.action_id, p.action_name, p.lat, p.lon,
+                -- This lines are so we can get the list of kpis vvvvvvvvvv
+                        GROUP_CONCAT(distinct t_p_list.plan_id) as plan_id_list,
+                        GROUP_CONCAT(distinct t_p_list.kpi_id) as kpi_id_list,
+                        GROUP_CONCAT(distinct t_p_list.kpi_name) as kpi_name_list,
+                        GROUP_CONCAT(t_p_list.absolute) as kpi_absolute_list,
+                        GROUP_CONCAT(t_p_list.relative) as kpi_relative_list,
+                        GROUP_CONCAT(t_p_list.geographical) as kpi_geographical_list,
+                -- This lines are so we can get the list of kpis ^^^^^^^^^^
+                p.popularity as popularity, p.category as category
+                FROM action p
+        -- This lines are so we can get the list of kpis vvvvvvvvvv
+                        INNER JOIN chosen_action t_list ON t_list.action_id = p.action_id
+                        INNER JOIN kpi_action_plan_detail t_p_list ON t_list.plan_id = t_p_list.plan_id
+                        INNER JOIN action a ON a.action_id = t_p_list.action_id
+        -- This lines are so we can get the list of kpis ^^^^^^^^^^
+                WHERE t_p_list.kpi_id = kpi
+		GROUP BY p.action_id
+                ORDER BY popularity DESC
+                LIMIT 100;
+		
+        ELSEIF (not(action is NULL))  THEN
+                SELECT t_p_list.plan_id, p.action_id, p.action_name, p.lat, p.lon,
+                -- This lines are so we can get the list of kpis vvvvvvvvvv
+                        GROUP_CONCAT(distinct t_p_list.plan_id) as plan_id_list,
+                        GROUP_CONCAT(distinct t_p_list.kpi_id) as kpi_id_list,
+                        GROUP_CONCAT(distinct t_p_list.kpi_name) as kpi_name_list,
+                        GROUP_CONCAT(t_p_list.absolute) as kpi_absolute_list,
+                        GROUP_CONCAT(t_p_list.relative) as kpi_relative_list,
+                        GROUP_CONCAT(t_p_list.geographical) as kpi_geographical_list,
+                -- This lines are so we can get the list of kpis ^^^^^^^^^^
+                p.popularity as popularity, p.category as category
+                FROM action p
+        -- This lines are so we can get the list of kpis vvvvvvvvvv
+                        INNER JOIN chosen_action t_list ON t_list.action_id = p.action_id
+                        INNER JOIN kpi_action_plan_detail t_p_list ON t_list.plan_id = t_p_list.plan_id
+                        INNER JOIN action a ON a.action_id = t_p_list.action_id
+        -- This lines are so we can get the list of kpis ^^^^^^^^^^
+		WHERE t_list.action_id = action
+                GROUP BY p.action_id
+                ORDER BY popularity DESC
+                LIMIT 100;
+
+	ELSE 
+                SELECT t_p_list.plan_id, p.action_id, p.action_name, p.lat, p.lon,
+                -- This lines are so we can get the list of kpis vvvvvvvvvv
+                        GROUP_CONCAT(distinct t_p_list.plan_id) as plan_id_list,
+                        GROUP_CONCAT(distinct t_p_list.kpi_id) as kpi_id_list,
+                        GROUP_CONCAT(distinct t_p_list.kpi_name) as kpi_name_list,
+                        GROUP_CONCAT(t_p_list.absolute) as kpi_absolute_list,
+                        GROUP_CONCAT(t_p_list.relative) as kpi_relative_list,
+                        GROUP_CONCAT(t_p_list.geographical) as kpi_geographical_list,
+                -- This lines are so we can get the list of kpis ^^^^^^^^^^
+                p.popularity as popularity, p.category as category
+                FROM action p
+        -- This lines are so we can get the list of kpis vvvvvvvvvv
+                        INNER JOIN chosen_action t_list ON t_list.action_id = p.action_id
+                        INNER JOIN kpi_action_plan_detail t_p_list ON t_list.plan_id = t_p_list.plan_id
+                        INNER JOIN action a ON a.action_id = t_p_list.action_id
+		WHERE t_list.action_id = action AND t_p_list.kpi_id = kpi
+		GROUP BY p.action_id
+                ORDER BY popularity DESC
+                LIMIT 100;
+
+
+
+         END IF;
+END$$
+DELIMITER ;
+
-- 
GitLab