forked from NASCOP-VLEID/VL-DASHBOARD
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathnew.sql
80 lines (71 loc) · 3.92 KB
/
new.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
DROP PROCEDURE IF EXISTS `proc_get_vl_subcounty_details`;
DELIMITER //
CREATE PROCEDURE `proc_get_vl_subcounty_details`
(IN filter_county INT(11), IN filter_year INT(11), IN from_month INT(11), IN to_year INT(11), IN to_month INT(11))
BEGIN
SET @QUERY = "SELECT
`countys`.`name` AS `county`,
`districts`.`name` AS `subcounty`,
SUM(`vcs`.`alltests`) AS `tests`,
SUM(`vcs`.`sustxfail`) AS `sustxfail`,
SUM(`vcs`.`confirmtx`) AS `confirmtx`,
SUM(`vcs`.`rejected`) AS `rejected`,
SUM(`vcs`.`adults`) AS `adults`,
SUM(`vcs`.`paeds`) AS `paeds`,
SUM(`vcs`.`maletest`) AS `maletest`,
SUM(`vcs`.`femaletest`) AS `femaletest`,
AVG(`vcs`.`sitessending`) AS `sitessending` FROM `vl_subcounty_summary` `vcs`
JOIN `districts` ON `vcs`.`subcounty` = `districts`.`ID`
JOIN `countys` ON `countys`.`ID` = `districts`.`county`
WHERE 1 ";
IF (from_month != 0 && from_month != '') THEN
IF (to_month != 0 && to_month != '' && filter_year = to_year) THEN
SET @QUERY = CONCAT(@QUERY, " AND `year` = '",filter_year,"' AND `month` BETWEEN '",from_month,"' AND '",to_month,"' ");
ELSE IF(to_month != 0 && to_month != '' && filter_year != to_year) THEN
SET @QUERY = CONCAT(@QUERY, " AND ((`year` = '",filter_year,"' AND `month` >= '",from_month,"') OR (`year` = '",to_year,"' AND `month` <= '",to_month,"') OR (`year` > '",filter_year,"' AND `year` < '",to_year,"')) ");
ELSE
SET @QUERY = CONCAT(@QUERY, " AND `year` = '",filter_year,"' AND `month`='",from_month,"' ");
END IF;
END IF;
ELSE
SET @QUERY = CONCAT(@QUERY, " AND `year` = '",filter_year,"' ");
END IF;
SET @QUERY = CONCAT(@QUERY, " AND `districts`.`county` = '",filter_county,"' GROUP BY `districts`.`ID` ORDER BY `tests` DESC ");
PREPARE stmt FROM @QUERY;
EXECUTE stmt;
END //
DELIMITER ;
DROP PROCEDURE IF EXISTS `proc_get_vl_county_details`;
DELIMITER //
CREATE PROCEDURE `proc_get_vl_county_details`
(IN filter_year INT(11), IN from_month INT(11), IN to_year INT(11), IN to_month INT(11))
BEGIN
SET @QUERY = "SELECT
`countys`.`name` AS `county`,
SUM(`vcs`.`alltests`) AS `tests`,
SUM(`vcs`.`sustxfail`) AS `sustxfail`,
SUM(`vcs`.`confirmtx`) AS `confirmtx`,
SUM(`vcs`.`rejected`) AS `rejected`,
SUM(`vcs`.`adults`) AS `adults`,
SUM(`vcs`.`paeds`) AS `paeds`,
SUM(`vcs`.`maletest`) AS `maletest`,
SUM(`vcs`.`femaletest`) AS `femaletest`,
AVG(`vcs`.`sitessending`) AS `sitessending` FROM `vl_county_summary` `vcs`
JOIN `countys` ON `vcs`.`county` = `countys`.`ID` WHERE 1";
IF (from_month != 0 && from_month != '') THEN
IF (to_month != 0 && to_month != '' && filter_year = to_year) THEN
SET @QUERY = CONCAT(@QUERY, " AND `year` = '",filter_year,"' AND `month` BETWEEN '",from_month,"' AND '",to_month,"' ");
ELSE IF(to_month != 0 && to_month != '' && filter_year != to_year) THEN
SET @QUERY = CONCAT(@QUERY, " AND ((`year` = '",filter_year,"' AND `month` >= '",from_month,"') OR (`year` = '",to_year,"' AND `month` <= '",to_month,"') OR (`year` > '",filter_year,"' AND `year` < '",to_year,"')) ");
ELSE
SET @QUERY = CONCAT(@QUERY, " AND `year` = '",filter_year,"' AND `month`='",from_month,"' ");
END IF;
END IF;
ELSE
SET @QUERY = CONCAT(@QUERY, " AND `year` = '",filter_year,"' ");
END IF;
SET @QUERY = CONCAT(@QUERY, " GROUP BY `countys`.`name` ORDER BY `tests` DESC ");
PREPARE stmt FROM @QUERY;
EXECUTE stmt;
END //
DELIMITER ;