forked from I-TECH-UW/EID-DASHBOARD
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathneweid.sql
109 lines (94 loc) · 5.1 KB
/
neweid.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
DROP PROCEDURE IF EXISTS `proc_get_eid_countys_details`;
DELIMITER //
CREATE PROCEDURE `proc_get_eid_countys_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(`tests`) AS `tests`,
`countys`.`pmtctneed1617` AS `pmtctneed`,
SUM(`actualinfants`) AS `actualinfants`,
SUM(`confirmdna` + `repeatspos`) AS `confirmdna`,
SUM(`actualinfantsPOS`) AS `positive`,
SUM(`actualinfants`-`actualinfantsPOS`) AS `negative`,
SUM(`redraw`) AS `redraw`,
SUM(`adults`) AS `adults`,
SUM(`adultsPOS`) AS `adultspos`,
AVG(`medage`) AS `medage`,
AVG(`sitessending`) AS `sitessending`,
SUM(`rejected`) AS `rejected`,
SUM(`infantsless2w`) AS `infantsless2w`,
SUM(`infantsless2wPOS`) AS `infantsless2wpos`,
SUM(`infantsless2m`) AS `infantsless2m`,
SUM(`infantsless2mPOS`) AS `infantsless2mpos`,
SUM(`infantsabove2m`) AS `infantsabove2m`,
SUM(`infantsabove2mPOS`) AS `infantsabove2mpos`
FROM `county_summary`
LEFT JOIN `countys` ON `county_summary`.`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 `county_summary`.`county` ORDER BY `tests` DESC ");
PREPARE stmt FROM @QUERY;
EXECUTE stmt;
END //
DELIMITER ;
DROP PROCEDURE IF EXISTS `proc_get_eid_county_subcounties_details`;
DELIMITER //
CREATE PROCEDURE `proc_get_eid_county_subcounties_details`
(IN C_id 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
`d`.`name` AS `subcounty`,
`c`.`name` AS `county`,
SUM(`tests`) AS `tests`,
SUM(`actualinfants`) AS `actualinfants`,
SUM(`confirmdna` + `repeatspos`) AS `confirmdna`,
SUM(`actualinfantsPOS`) AS `positive`,
SUM(`actualinfants`-`actualinfantsPOS`) AS `negative`,
SUM(`redraw`) AS `redraw`,
SUM(`adults`) AS `adults`,
SUM(`adultsPOS`) AS `adultspos`,
AVG(`medage`) AS `medage`,
AVG(`sitessending`) AS `sitessending`,
SUM(`rejected`) AS `rejected`,
SUM(`infantsless2w`) AS `infantsless2w`,
SUM(`infantsless2wPOS`) AS `infantsless2wpos`,
SUM(`infantsless2m`) AS `infantsless2m`,
SUM(`infantsless2mPOS`) AS `infantsless2mpos`,
SUM(`infantsabove2m`) AS `infantsabove2m`,
SUM(`infantsabove2mPOS`) AS `infantsabove2mpos` ";
IF (from_month != 0 && from_month != '') THEN
SET @QUERY = CONCAT(@QUERY, " FROM `subcounty_summary` `scs` ");
ELSE
SET @QUERY = CONCAT(@QUERY, " FROM `subcounty_summary_yearly` `scs` ");
END IF;
SET @QUERY = CONCAT(@QUERY, " LEFT JOIN `districts` `d` ON `scs`.`subcounty` = `d`.`ID`
LEFT JOIN `countys` `c` ON `d`.`county` = `c`.`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, " AND `c`.`ID` = '",C_id,"' ");
SET @QUERY = CONCAT(@QUERY, " GROUP BY `subcounty` ORDER BY `tests` DESC ");
PREPARE stmt FROM @QUERY;
EXECUTE stmt;
END //
DELIMITER ;