From 6a877db87177d2eac5f8947d5021f6d644627891 Mon Sep 17 00:00:00 2001
From: "Campos Cordobes, Sergio" <sergio.campos@tecnalia.com>
Date: Mon, 24 Apr 2023 08:15:27 +0200
Subject: [PATCH] Update .recommender_plans.sql.swp,
 recommender_kpis_kpi_id.sql, recommender_plans.sql,
 recommender_popularity.sql, recommender_popularity_detailed.sql,
 recommender_preferences.sql

---
 .recommender_plans.sql.swp          | Bin 0 -> 16384 bytes
 recommender_kpis_kpi_id.sql         |  13 ++++
 recommender_plans.sql               | 100 ++++++++++++++++++++++++++++
 recommender_popularity.sql          |  46 +++++++++++++
 recommender_popularity_detailed.sql | 100 ++++++++++++++++++++++++++++
 recommender_preferences.sql         |  63 ++++++++++++++++++
 6 files changed, 322 insertions(+)
 create mode 100644 .recommender_plans.sql.swp
 create mode 100644 recommender_kpis_kpi_id.sql
 create mode 100644 recommender_plans.sql
 create mode 100644 recommender_popularity.sql
 create mode 100644 recommender_popularity_detailed.sql
 create mode 100644 recommender_preferences.sql

diff --git a/.recommender_plans.sql.swp b/.recommender_plans.sql.swp
new file mode 100644
index 0000000000000000000000000000000000000000..728a4bd483c5a2b5bc6898110584c59db20579df
GIT binary patch
literal 16384
zcmeI2PiP}m9LL|TRyXRZxF{-!Pu*o(ZT>U`DJhj^lWCi!P1ZC~WMPw+nb~x3Iy25p
zx>OH>7e&3eptvW&y?IeV5LOgCiF)uLD(FQJyeOXZB(i>AcIIW;G}%mB#XoQ0)0ug1
z@|*X*^P69Ozn8W$kCj&FOe#yT-Ac$eMpira+wJ7N7Y`EMGfdXv9m6+IRg)dJ#Vo_u
zhh3Sr9X}mj*kF#4*0ffu)$ml;aC|TA>Z-lIu3MV!HXM^#UdnsQ9A;4VNd=?=qbbnw
z&n9OM*Qc{n8UBF>?!S*7xvf6hXXRB=0jYpgKq?>=kP1izqyka_slb0i0pB@Dp2bWK
z2D6z9_6N4@uL;hN2m431^#`YNCl!ziNCl(<QUR%eR6r^q6_5%@1*8H}0ja=0Pywb9
zlEb)8@`4|~|A+PekGBx=F*pxi0qdX%#=zy93HcP90}p|34-@hdm;t}uM94cp19yV2
zZY1P=;DNiqyEhQRz&+q{f{?Gl2jCL802aaThX{EKoCb%$rRxbX!5!eU>j-%s7~mNA
ze2fqk906ZkOUOm=GEl)1P{7^b6D(jn10Dq5VX@-{kOMzs-md{Z|0LI7=tg5~$t10<
z86GtaOZO;qb?VvloK96{(YEeWe@#c9=TrMEebT{&XT(MZ-Da^`sh?^TD&<1HHtsi^
z1~-su>vr2^&YGbzbAmFDn`=ZHyO&MIwCd_6^NkH%SPUC>WsS8w+wAx~YhgniYZ_*2
zSSqv&r_N_;vGtx=ezJFyh9+ihXnQlq|B<B;*b9ya7>w;O8)EBww5!UpQmFNG3hq2f
zofK1j!{$SZoN(3nDV}(DXInjQQI=Pg(gGc~Y=8X9XDOVuTwh+En4q+_q?Ab_(KmqQ
z(n_gDr!tw@zOG8Os8s2RC#YjP9h13+ze$VAY5``t{@v~B4YTj_k1r`zr8hP10)|HC
zxYvtoV{0kiy)(iF-L{p=WsLh|rBtSY4vbcC+#4zeAv{yaJc>O&hu{O<C#C5=GfZKO
z-^TBHO)7Yh(K`iAqufVbvpwAkZWWk}7#CxP`G}(k%du4z4!R+1&|q7rR#rkCoxWZt
z6>$zkwB8x_PnG$4+jcj3tP79(OvE1S2reMwuZ+LFFYF=SJt+Q0-m0sF-$XR-B0lh-
zYjsRh1l>M|-%ZF3UPd}R^8OXCl8mOYRUP7Q|C(qTe8=xg4m-$Jn5c>zE^OE#@fu_!
zOizW4uwe%q;Y-B#TfQM=zPhEr8REZtkC)*?;Hm0(G+t6>X>?HLdO}$&l}Vwh<ZB9&
z(1j9Gyt$@p+kSJtY1$~LkTLm9da7C}D8)MR&Su|#sW-=w5%N2h%C$-S39}lUPLQI~
z4a-Ozj~*qOYdh3&ZB^Ggs0Vj2HH#`VN%I<Wd=y`{<>hE5o6csk({!G7{59Ln(N*1T
z8#XPlb;tJj<)SBVX|`q1yvwXMZeY=R%g|km@rw$sj;u+eww0q(52rIzU8{uG{~grT
zNz~cA{y!enzaOH`e*>Hb2fzi?`6Vz0en6f75qK3m4K{%b?gd|>=6?=MgYQxAp9e+o
z3+nwhK?CH$AE@&$f?4nr?)^4+9oV1-RzVd!3g$r$qyQi1B#@g_Kq?>=kP1izqym4J
z0=;ZzmMG;SQkpRBqBy0c1>`TJ*A{f{rZ63_7MMtctAE{<@92Wnm1CO6IK(0xUsvl}
z8x%th4rC?zzpO-~Zm^|l6W$a32}RU_(Z)Enps*Siqrzg?uq$g}St_iB4RNf=I(4sg
uYIM1Ll!ZOIKT$I3!~A&1-%qX@Gf8MBb{rUC$y`R)*s2a$C)xLPzCQua2Iq<Z

literal 0
HcmV?d00001

diff --git a/recommender_kpis_kpi_id.sql b/recommender_kpis_kpi_id.sql
new file mode 100644
index 0000000..95b2406
--- /dev/null
+++ b/recommender_kpis_kpi_id.sql
@@ -0,0 +1,13 @@
+CREATE DEFINER=`root`@`localhost` PROCEDURE `recommender_kpis_kpi_id`(IN kpi_id INT)
+BEGIN 
+SELECT kpi.id, kpi_name, count(*) as Count, kpi.popularity as Popularity, count(*)/kpi.popularity as Probability FROM kpi kpi
+	INNER JOIN (
+	SELECT DISTINCT t_p.action_id, t_p.kpi_id, c_p.plan_id, r.time_slot FROM kpi_action t_p
+		INNER JOIN CHOSEN_action c_p ON c_p.action_id = t_p.action_id
+		INNER JOIN plan_detail r ON r.plan_id = c_p.plan_id AND r.time_slot = c_p.time_slot
+		INNER JOIN CHOSEN_action target_p ON r.plan_id = target_p.plan_id AND r.time_slot = target_p.time_slot
+		INNER JOIN kpi_action target_kpi_p ON target_kpi_p.kpi_id != t_p.kpi_id AND target_p.action_id = target_kpi_p.action_id 
+		WHERE target_kpi_p.kpi_id = kpi_id) as distinct_kpis ON kpi.id = distinct_kpis.kpi_id
+	GROUP BY kpi.id
+	ORDER BY Probability DESC;
+END
diff --git a/recommender_plans.sql b/recommender_plans.sql
new file mode 100644
index 0000000..05f2fd1
--- /dev/null
+++ b/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 ;
+
diff --git a/recommender_popularity.sql b/recommender_popularity.sql
new file mode 100644
index 0000000..d66e20a
--- /dev/null
+++ b/recommender_popularity.sql
@@ -0,0 +1,46 @@
+drop procedure recommender_popularity;
+DELIMITER $$
+CREATE DEFINER=`root`@`localhost` PROCEDURE `recommender_popularity`(IN kpi INT)
+BEGIN
+	IF kpi is null THEN
+		SELECT 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_list.id) as kpi_id_list,
+			GROUP_CONCAT(distinct t_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 kpi_action t_p_list ON t_p_list.action_id = p.action_id 
+			INNER JOIN kpi t_list ON t_list.id = t_p_list.kpi_id
+        -- This lines are so we can get the list of kpis ^^^^^^^^^^
+        GROUP BY p.action_id
+		ORDER BY popularity DESC
+		LIMIT 100;
+	ELSE
+		SELECT 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_list.id) as kpi_id_list,
+			GROUP_CONCAT(distinct t_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
+        INNER JOIN kpi_action t ON t.action_id = p.action_id
+        -- This lines are so we can get the list of kpis vvvvvvvvvv
+			INNER JOIN kpi_action t_p_list ON t_p_list.action_id = p.action_id 
+			INNER JOIN kpi t_list ON t_list.id = t_p_list.kpi_id
+        -- This lines are so we can get the list of kpis ^^^^^^^^^^
+        WHERE t.kpi_id = kpi
+        GROUP BY p.action_id
+		ORDER BY popularity DESC
+		LIMIT 100;
+	END IF;
+END$$
+DELIMITER ;
+
diff --git a/recommender_popularity_detailed.sql b/recommender_popularity_detailed.sql
new file mode 100644
index 0000000..05f2fd1
--- /dev/null
+++ b/recommender_popularity_detailed.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 ;
+
diff --git a/recommender_preferences.sql b/recommender_preferences.sql
new file mode 100644
index 0000000..90536b3
--- /dev/null
+++ b/recommender_preferences.sql
@@ -0,0 +1,63 @@
+-- Created on: 16/01/2023
+-- @author: Andoni Aranguren Ubierna
+-- Adaptations: 02/2023 @author: Sergio Campos
+
+drop procedure recommender_preferences;
+DELIMITER $$
+CREATE DEFINER=`root`@`localhost` PROCEDURE `recommender_preferences`(
+	IN emission_total FLOAT,
+    IN noise_total FLOAT,
+    IN kpi INT)
+BEGIN 
+	IF kpi IS NULL THEN
+		SELECT c_action.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_list.id) as kpi_id_list,
+			GROUP_CONCAT(distinct t_list.kpi_name) as kpi_name_list,
+		--  This lines are so we can get the list of kpis ^^^^^^^^^^
+		COUNT(c_action.action_id) as Count
+		FROM chosen_action c_action
+		INNER JOIN           
+			(SELECT r.plan_id, 
+					r.time_slot, 
+					POWER(r.emission_total-emission_total,2) + POWER(r.noise_total-noise_total,2) as distance
+			FROM plan_detail r
+			ORDER BY distance ASC
+			LIMIT 5) as r ON c_action.plan_id = r.plan_id AND c_action.time_slot = r.time_slot
+		INNER JOIN action p ON c_action.action_id = p.action_id 
+        -- This lines are so we can get the list of kpis vvvvvvvvvv
+			INNER JOIN kpi_action t_p_list ON t_p_list.action_id = p.action_id 
+			INNER JOIN kpi t_list ON t_list.id = t_p_list.kpi_id
+		--  This lines are so we can get the list of kpis ^^^^^^^^^^
+		GROUP BY c_action.action_id
+		ORDER BY Count DESC
+		LIMIT 100;
+	ELSE
+		SELECT c_action.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_list.id) as kpi_id_list,
+			GROUP_CONCAT(distinct t_list.kpi_name) as kpi_name_list,
+		--  This lines are so we can get the list of kpis ^^^^^^^^^^
+		COUNT(c_action.action_id) as Count
+		FROM chosen_action c_action
+		INNER JOIN           
+			(SELECT r.plan_id, 
+					r.time_slot, 
+					POWER(r.emission_total-emission_total,2) + POWER(r.noise_total-noise_total,2) as distance
+			FROM plan_detail r
+			ORDER BY distance ASC
+			LIMIT 5) as r ON c_action.plan_id = r.plan_id AND c_action.time_slot = r.time_slot
+		INNER JOIN action p ON c_action.action_id = p.action_id 
+        INNER JOIN kpi_action t ON t.action_id = p.action_id
+        -- This lines are so we can get the list of kpis vvvvvvvvvv
+			INNER JOIN kpi_action t_p_list ON t_p_list.action_id = p.action_id 
+			INNER JOIN kpi t_list ON t_list.id = t_p_list.kpi_id
+		--  This lines are so we can get the list of kpis ^^^^^^^^^^
+        WHERE t.kpi_id = kpi
+		GROUP BY c_action.action_id
+		ORDER BY Count DESC
+		LIMIT 100;
+	END IF;
+END$$
+DELIMITER ;
+
-- 
GitLab