select count(*) from kpi_report_upf


CREATE VIEW kpi_report_all AS
SELECT * FROM kpi_report_udm
UNION ALL
SELECT * FROM kpi_report_amf
UNION ALL
SELECT * FROM kpi_report_upf;


select DISTINCT(ne_type) from kpi_title 

SELECT DISTINCT LOWER(ne_type) FROM kpi_title

SELECT JSON_EXTRACT(kpi_values, CONCAT('$[', @path, '].', @key_to_search)) AS value FROM kpi_report_upf where `date`="2024-04-26";


SELECT JSON_EXTRACT(JSON_CONTAINS(kpi_values, '{"kpi_id": "UPF.01"}') FROM kpi_report_upf where `date`="2024-04-26";


SELECT
JSON_EXTRACT(kpi_values, CONCAT('$[', JSON_UNQUOTE(JSON_SEARCH(kpi_values, 'kpi_id', 'UPF.01')), '].value')) AS value_UPF01,
JSON_EXTRACT(kpi_values, CONCAT('$[', JSON_UNQUOTE(JSON_SEARCH(kpi_values, 'kpi_id', 'UPF.03')), '].value')) AS value_UPF03
FROM
kpi_report_upf
WHERE
JSON_SEARCH(kpi_values, 'kpi_id', 'UPF.01') IS NOT NULL
AND JSON_SEARCH(kpi_values, 'kpi_id', 'UPF.03') IS NOT NULL;


SELECT
JSON_EXTRACT(kpi_values, CONCAT('$[', JSON_UNQUOTE(JSON_SEARCH(kpi_values, 'all', 'UPF.01')), '].value')) AS value_UPF01
FROM
kpi_report_upf
WHERE
JSON_CONTAINS(kpi_values, '{"kpi_id": "UPF.01"}');


SELECT
COALESCE(JSON_EXTRACT(kpi_values, CONCAT('$[', JSON_UNQUOTE(JSON_SEARCH(kpi_values, 'all', 'UPF.01')), '].value')), 0) AS value_UPF01
FROM
kpi_report_upf
WHERE
JSON_CONTAINS(kpi_values, '{"kpi_id": "UPF.01"}');

SELECT
	CONCAT(
		DATE_FORMAT( gk.start_time, '%H:' ),
		LPAD( FLOOR( MINUTE ( gk.start_time ) / 15 ) * 15, 2, '0' ) 
	) AS timeGroup,
	min( CASE WHEN gk.INDEX != '' THEN gk.INDEX ELSE 0 END ) AS startIndex,
	min( CASE WHEN gk.ne_name != '' THEN gk.ne_name ELSE 0 END ) AS neName,
	sum( CASE WHEN JSON_CONTAINS(kpi_values,JSON_OBJECT('kpi_id', "UPF.01")) THEN gk.kpi_values->'$[*].value ELSE 0 END ) AS 'UPF.01',
	sum( CASE WHEN JSON_CONTAINS(kpi_values,JSON_OBJECT('kpi_id', "UPF.02")) THEN gk.kpi_values->'$[*].value ELSE 0 END ) AS 'UPF.02',
	sum( CASE WHEN gk.kpi_id = 'UPF.03' THEN gk.VALUE ELSE 0 END ) AS 'UPF.03',
	sum( CASE WHEN gk.kpi_id = 'UPF.04' THEN gk.VALUE ELSE 0 END ) AS 'UPF.04',
	sum( CASE WHEN gk.kpi_id = 'UPF.05' THEN gk.VALUE ELSE 0 END ) AS 'UPF.05',
	sum( CASE WHEN gk.kpi_id = 'UPF.06' THEN gk.VALUE ELSE 0 END ) AS 'UPF.06',
	sum( CASE WHEN gk.kpi_id = 'UPF.07' THEN gk.VALUE ELSE 0 END ) AS 'UPF.07',
	sum( CASE WHEN gk.kpi_id = 'UPF.08' THEN gk.VALUE ELSE 0 END ) AS 'UPF.08',
	sum( CASE WHEN gk.kpi_id = 'UPF.09' THEN gk.VALUE ELSE 0 END ) AS 'UPF.09',
	sum( CASE WHEN gk.kpi_id = 'UPF.10' THEN gk.VALUE ELSE 0 END ) AS 'UPF.10',
	sum( CASE WHEN gk.kpi_id = 'UPF.14' THEN gk.VALUE ELSE 0 END ) AS 'UPF.14',
	sum( CASE WHEN gk.kpi_id = 'UPF.11' THEN gk.VALUE ELSE 0 END ) AS 'UPF.11',
	sum( CASE WHEN gk.kpi_id = 'UPF.12' THEN gk.VALUE ELSE 0 END ) AS 'UPF.12',
	sum( CASE WHEN gk.kpi_id = 'UPF.13' THEN gk.VALUE ELSE 0 END ) AS 'UPF.13',
	sum( CASE WHEN gk.kpi_id = 'UPF.15' THEN gk.VALUE ELSE 0 END ) AS 'UPF.15' 
FROM
	kpi_report_upf gk 
WHERE
	gk.rm_uid = '4400HX1UPF001'
	AND gk.date = "2024-04-26"
	AND gk.start_time >= '00:00:00' 
	AND gk.start_time <= '23:59:59' 
GROUP BY
	timeGroup 
ORDER BY
	timeGroup DESC



SELECT
	CONCAT(
		DATE_FORMAT( gk.start_time, '%H:' ),
		LPAD( FLOOR( MINUTE ( gk.start_time ) / 15 ) * 15, 2, '0' ) 
	) AS timeGroup,
	min( CASE WHEN gk.INDEX != '' THEN gk.INDEX ELSE 0 END ) AS startIndex,
	min( CASE WHEN gk.ne_name != '' THEN gk.ne_name ELSE 0 END ) AS neName,
	SUM(CASE WHEN JSON_EXTRACT(gk.kpi_values, '$[0].kpi_id') = "UPF.01" THEN JSON_EXTRACT(gk.kpi_values, '$[0].value') ELSE 0 END) AS 'UPF.01',
	SUM(CASE WHEN JSON_EXTRACT(gk.kpi_values, '$[1].kpi_id') = "UPF.02" THEN JSON_EXTRACT(gk.kpi_values, '$[1].value') ELSE 0 END) AS 'UPF.02',
	SUM(CASE WHEN JSON_EXTRACT(gk.kpi_values, '$[2].kpi_id') = "UPF.03" THEN JSON_EXTRACT(gk.kpi_values, '$[2].value') ELSE 0 END) AS 'UPF.03'
FROM
	kpi_report_upf gk 
WHERE
	gk.rm_uid = '4400HX1UPF001'
	AND gk.date = "2024-05-08"
	AND gk.start_time >= '10:55:00' 
	AND gk.start_time <= '23:59:59' 
GROUP BY
	timeGroup 
ORDER BY
	timeGroup DESC


SELECT
	CONCAT(
		DATE_FORMAT( gk.start_time, '%H:' ),
		LPAD( FLOOR( MINUTE ( gk.start_time ) / 15 ) * 15, 2, '0' ) 
	) AS timeGroup,
	min( CASE WHEN gk.INDEX != '' THEN gk.INDEX ELSE 0 END ) AS startIndex,
	min( CASE WHEN gk.ne_name != '' THEN gk.ne_name ELSE 0 END ) AS neName,
	SUM(JSON_EXTRACT(gk.kpi_values, '$[0].`UPF.01`')) AS 'UPF.01',
	SUM(JSON_EXTRACT(gk.kpi_values, '$[1].`UPF.02`')) AS 'UPF.02',
	SUM(JSON_EXTRACT(gk.kpi_values, '$[2].`UPF.03`')) AS 'UPF.03'
FROM
	kpi_report_upf gk 
WHERE
	gk.rm_uid = '4400HX1UPF001'
	AND gk.date = "2024-05-08"
	AND gk.start_time >= '10:53:00' 
	AND gk.start_time <= '10:59:59' 
GROUP BY
	timeGroup 
ORDER BY
	timeGroup DESC
	

SELECT
	min( CASE WHEN gk.INDEX != '' THEN gk.INDEX ELSE 0 END ) AS startIndex,
	min( CASE WHEN gk.ne_name != '' THEN gk.ne_name ELSE 0 END ) AS neName,
	SUM(CASE WHEN JSON_EXTRACT(gk.kpi_values, '$[*].kpi_id') = 'UPF.01' THEN JSON_EXTRACT(gk.kpi_values, '$[*].value') ELSE 0 END) AS 'UPF.01',
	SUM(CASE WHEN JSON_EXTRACT(gk.kpi_values, '$[*].kpi_id') = 'UPF.02' THEN JSON_EXTRACT(gk.kpi_values, '$[*].value') ELSE 0 END) AS 'UPF.02',
	SUM(CASE WHEN JSON_EXTRACT(gk.kpi_values, '$[*].kpi_id') = 'UPF.03' THEN JSON_EXTRACT(gk.kpi_values, '$[*].value') ELSE 0 END) AS 'UPF.03'
FROM
	kpi_report_upf gk 
WHERE
	gk.rm_uid = '4400HX1UPF001'
	AND gk.date = "2024-05-03"
	AND gk.start_time >= '00:00:00' 
	AND gk.start_time <= '23:59:59' 


SELECT 
	CONCAT( DATE_FORMAT(CONCAT(gk.`date`, " ", gk.start_time), '%Y-%m-%d %H:'), LPAD(FLOOR(MINUTE(gk.start_time) / 15) * 15, 2, '0') ) AS timeGroup,
	min(CASE WHEN gk.index != '' THEN gk.index ELSE 0 END) AS startIndex,
	min(CASE WHEN gk.ne_type != '' THEN gk.ne_type ELSE 0 END) AS neType,
	min(CASE WHEN gk.ne_name != '' THEN gk.ne_name ELSE 0 END) AS neName,
	sum(CASE WHEN JSON_EXTRACT(gk.kpi_values, '$[0].kpi_id') = 'UPF.01' THEN JSON_EXTRACT(gk.kpi_values, '$[0].value') ELSE 0 END) AS 'UPF.01',
	sum(CASE WHEN JSON_EXTRACT(gk.kpi_values, '$[1].kpi_id') = 'UPF.02' THEN JSON_EXTRACT(gk.kpi_values, '$[1].value') ELSE 0 END) AS 'UPF.02',
	sum(CASE WHEN JSON_EXTRACT(gk.kpi_values, '$[2].kpi_id') = 'UPF.03' THEN JSON_EXTRACT(gk.kpi_values, '$[2].value') ELSE 0 END) AS 'UPF.03',
	sum(CASE WHEN JSON_EXTRACT(gk.kpi_values, '$[3].kpi_id') = 'UPF.04' THEN JSON_EXTRACT(gk.kpi_values, '$[3].value') ELSE 0 END) AS 'UPF.04',
	sum(CASE WHEN JSON_EXTRACT(gk.kpi_values, '$[4].kpi_id') = 'UPF.05' THEN JSON_EXTRACT(gk.kpi_values, '$[4].value') ELSE 0 END) AS 'UPF.05',
	sum(CASE WHEN JSON_EXTRACT(gk.kpi_values, '$[5].kpi_id') = 'UPF.06' THEN JSON_EXTRACT(gk.kpi_values, '$[5].value') ELSE 0 END) AS 'UPF.06',
	sum(CASE WHEN JSON_EXTRACT(gk.kpi_values, '$[6].kpi_id') = 'UPF.07' THEN JSON_EXTRACT(gk.kpi_values, '$[6].value') ELSE 0 END) AS 'UPF.07',
	sum(CASE WHEN JSON_EXTRACT(gk.kpi_values, '$[7].kpi_id') = 'UPF.08' THEN JSON_EXTRACT(gk.kpi_values, '$[7].value') ELSE 0 END) AS 'UPF.08',
	sum(CASE WHEN JSON_EXTRACT(gk.kpi_values, '$[8].kpi_id') = 'UPF.09' THEN JSON_EXTRACT(gk.kpi_values, '$[8].value') ELSE 0 END) AS 'UPF.09',
	sum(CASE WHEN JSON_EXTRACT(gk.kpi_values, '$[9].kpi_id') = 'UPF.10' THEN JSON_EXTRACT(gk.kpi_values, '$[9].value') ELSE 0 END) AS 'UPF.10' 
FROM kpi_report_upf gk 
where gk.rm_uid = '4400HX1UPF001' and gk.ne_type = 'UPF' and CONCAT(gk.`date`, " ", gk.start_time) >= '2024-05-08 10:00:00' and CONCAT(gk.`date`, " ", gk.start_time) <= '2024-05-08 23:59:59' 
GROUP BY timeGroup order by timeGroup desc



DELETE t1
FROM ne_state t1
JOIN (
SELECT `timestamp`
FROM ne_state where ne_type='upf'
ORDER BY id DESC
LIMIT 999999 OFFSET 3
) t2 ON t1.`timestamp` = t2.`timestamp` and ne_type='upf';


select count(1) from ne_state where ne_type='upf'


CREATE TABLE IF NOT EXISTS kpi_report_test AS SELECT * FROM kpi_report WHERE 1=0;ALTER TABLE kpi_report_test MODIFY COLUMN `id` int(11) NOT NULL AUTO_INCREMENT FIRST,ADD PRIMARY KEY IF NOT EXISTS (`id`);

ALTER TABLE kpi_report_ims MODIFY COLUMN `id` int(11) NOT NULL AUTO_INCREMENT FIRST,ADD PRIMARY KEY IF NOT EXISTS (`id`)

SELECT * FROM kpi_report WHERE 1=0