-
Notifications
You must be signed in to change notification settings - Fork 128
/
01-obitos-setup.sql
289 lines (235 loc) · 9.98 KB
/
01-obitos-setup.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
DROP VIEW IF EXISTS deaths_brazil_2020_by_arpen_per_day;
CREATE VIEW deaths_brazil_2020_by_arpen_per_day AS
SELECT
date,
epidemiological_week_2020 AS epidemiological_week,
SUM(new_deaths_respiratory_failure_2020) AS new_deaths_respiratory_failure,
SUM(deaths_respiratory_failure_2020) AS deaths_respiratory_failure,
SUM(new_deaths_pneumonia_2020) AS new_deaths_pneumonia,
SUM(deaths_pneumonia_2020) AS deaths_pneumonia,
SUM(new_deaths_covid19) AS new_deaths_covid19,
SUM(deaths_covid19) AS deaths_covid19,
SUM(deaths_sars_2020) AS deaths_sars,
SUM(new_deaths_sars_2020) AS new_deaths_sars,
SUM(deaths_septicemia_2020) AS deaths_septicemia,
SUM(new_deaths_septicemia_2020) AS new_deaths_septicemia,
SUM(deaths_indeterminate_2020) AS deaths_indeterminate,
SUM(new_deaths_indeterminate_2020) AS new_deaths_indeterminate,
SUM(deaths_others_2020) AS deaths_others,
SUM(new_deaths_others_2020) AS new_deaths_others,
SUM(new_deaths_total_2020) AS new_deaths_total,
SUM(deaths_total_2020) AS deaths_total
FROM obito_cartorio
GROUP BY
date
ORDER BY
date;
DROP VIEW IF EXISTS deaths_brazil_by_arpen_per_day;
CREATE VIEW deaths_brazil_by_arpen_per_day AS
SELECT
date,
SUM(new_deaths_respiratory_failure_2020) AS new_deaths_respiratory_failure_2020,
SUM(deaths_respiratory_failure_2020) AS deaths_respiratory_failure_2020,
SUM(new_deaths_pneumonia_2020) AS new_deaths_pneumonia_2020,
SUM(deaths_pneumonia_2020) AS deaths_pneumonia_2020,
SUM(new_deaths_covid19) AS new_deaths_covid19_2020,
SUM(deaths_covid19) AS deaths_covid19_2020,
SUM(deaths_sars_2020) AS deaths_sars_2020,
SUM(new_deaths_sars_2020) AS new_deaths_sars_2020,
SUM(deaths_septicemia_2020) AS deaths_septicemia_2020,
SUM(new_deaths_septicemia_2020) AS new_deaths_septicemia_2020,
SUM(deaths_indeterminate_2020) AS deaths_indeterminate_2020,
SUM(new_deaths_indeterminate_2020) AS new_deaths_indeterminate_2020,
SUM(deaths_others_2020) AS deaths_others_2020,
SUM(new_deaths_others_2020) AS new_deaths_others_2020,
SUM(new_deaths_total_2020) AS new_deaths_total_2020,
SUM(deaths_total_2020) AS deaths_total_2020,
SUM(new_deaths_respiratory_failure_2019) AS new_deaths_respiratory_failure_2019,
SUM(deaths_respiratory_failure_2019) AS deaths_respiratory_failure_2019,
SUM(new_deaths_pneumonia_2019) AS new_deaths_pneumonia_2019,
SUM(deaths_pneumonia_2019) AS deaths_pneumonia_2019,
SUM(new_deaths_covid19) AS new_deaths_covid19_2019,
SUM(deaths_covid19) AS deaths_covid19_2019,
SUM(deaths_sars_2019) AS deaths_sars_2019,
SUM(new_deaths_sars_2019) AS new_deaths_sars_2019,
SUM(deaths_septicemia_2019) AS deaths_septicemia_2019,
SUM(new_deaths_septicemia_2019) AS new_deaths_septicemia_2019,
SUM(deaths_indeterminate_2019) AS deaths_indeterminate_2019,
SUM(new_deaths_indeterminate_2019) AS new_deaths_indeterminate_2019,
SUM(deaths_others_2019) AS deaths_others_2019,
SUM(new_deaths_others_2019) AS new_deaths_others_2019,
SUM(new_deaths_total_2019) AS new_deaths_total_2019,
SUM(deaths_total_2019) AS deaths_total_2019
FROM obito_cartorio
GROUP BY
date
ORDER BY
date;
DROP VIEW IF EXISTS deaths_by_arpen_per_week_2020;
CREATE VIEW deaths_by_arpen_per_week_2020 AS
SELECT
state,
epidemiological_week_2020 AS epidemiological_week,
SUM(new_deaths_respiratory_failure_2020) AS new_deaths_respiratory_failure,
MAX(deaths_respiratory_failure_2020) AS deaths_respiratory_failure,
SUM(new_deaths_pneumonia_2020) AS new_deaths_pneumonia,
MAX(deaths_pneumonia_2020) AS deaths_pneumonia,
SUM(new_deaths_covid19) AS new_deaths_covid19,
MAX(deaths_covid19) AS deaths_covid19,
SUM(new_deaths_sars_2020) AS new_deaths_sars,
MAX(deaths_sars_2020) AS deaths_sars,
SUM(new_deaths_septicemia_2020) AS new_deaths_septicemia,
MAX(deaths_septicemia_2020) AS deaths_septicemia,
SUM(new_deaths_indeterminate_2020) AS new_deaths_indeterminate,
MAX(deaths_indeterminate_2020) AS deaths_indeterminate,
SUM(new_deaths_others_2020) AS new_deaths_others,
MAX(deaths_others_2020) AS deaths_others,
SUM(new_deaths_total_2020) AS new_deaths_total,
MAX(deaths_total_2020) AS deaths_total
FROM obito_cartorio
GROUP BY
epidemiological_week,
state
ORDER BY
epidemiological_week,
state;
DROP VIEW IF EXISTS deaths_by_arpen_per_week_2019;
CREATE VIEW deaths_by_arpen_per_week_2019 AS
SELECT
state,
epidemiological_week_2019 AS epidemiological_week,
SUM(new_deaths_respiratory_failure_2019) AS new_deaths_respiratory_failure,
MAX(deaths_respiratory_failure_2019) AS deaths_respiratory_failure,
SUM(new_deaths_pneumonia_2019) AS new_deaths_pneumonia,
MAX(deaths_pneumonia_2019) AS deaths_pneumonia,
SUM(new_deaths_covid19) AS new_deaths_covid19,
MAX(deaths_covid19) AS deaths_covid19,
SUM(new_deaths_sars_2019) AS new_deaths_sars,
MAX(deaths_sars_2019) AS deaths_sars,
SUM(new_deaths_septicemia_2019) AS new_deaths_septicemia,
MAX(deaths_septicemia_2019) AS deaths_septicemia,
SUM(new_deaths_indeterminate_2019) AS new_deaths_indeterminate,
MAX(deaths_indeterminate_2019) AS deaths_indeterminate,
SUM(new_deaths_others_2019) AS new_deaths_others,
MAX(deaths_others_2019) AS deaths_others,
SUM(new_deaths_total_2019) AS new_deaths_total,
MAX(deaths_total_2019) AS deaths_total
FROM obito_cartorio
GROUP BY
epidemiological_week,
state
ORDER BY
epidemiological_week,
state;
DROP VIEW IF EXISTS deaths_by_arpen_per_week;
CREATE VIEW deaths_by_arpen_per_week AS
SELECT
c.state,
c.epidemiological_week,
c.new_deaths_respiratory_failure AS new_deaths_respiratory_failure_2020,
c.deaths_respiratory_failure AS deaths_respiratory_failure_2020,
l.new_deaths_respiratory_failure AS new_deaths_respiratory_failure_2019,
l.deaths_respiratory_failure AS deaths_respiratory_failure_2019,
c.new_deaths_pneumonia AS new_deaths_pneumonia_2020,
c.deaths_pneumonia AS deaths_pneumonia_2020,
l.new_deaths_pneumonia AS new_deaths_pneumonia_2019,
l.deaths_pneumonia AS deaths_pneumonia_2019,
c.new_deaths_covid19 AS new_deaths_covid19_2020,
c.deaths_covid19 AS deaths_covid19_2020,
l.new_deaths_covid19 AS new_deaths_covid19_2019,
l.deaths_covid19 AS deaths_covid19_2019,
c.new_deaths_sars AS new_deaths_sars_2020,
c.deaths_sars AS deaths_sars_2020,
l.new_deaths_sars AS new_deaths_sars_2019,
l.deaths_sars AS deaths_sars_2019,
c.new_deaths_septicemia AS new_deaths_septicemia_2020,
c.deaths_septicemia AS deaths_septicemia_2020,
l.new_deaths_septicemia AS new_deaths_septicemia_2019,
l.deaths_septicemia AS deaths_septicemia_2019,
c.new_deaths_indeterminate AS new_deaths_indeterminate_2020,
c.deaths_indeterminate AS deaths_indeterminate_2020,
l.new_deaths_indeterminate AS new_deaths_indeterminate_2019,
l.deaths_indeterminate AS deaths_indeterminate_2019,
c.new_deaths_others AS new_deaths_others_2020,
c.deaths_others AS deaths_others_2020,
l.new_deaths_others AS new_deaths_others_2019,
l.deaths_others AS deaths_others_2019,
c.new_deaths_total AS new_deaths_total_2020,
c.deaths_total AS deaths_total_2020,
l.new_deaths_total AS new_deaths_total_2019,
l.deaths_total AS deaths_total_2019
FROM deaths_by_arpen_per_week_2020 AS c
JOIN deaths_by_arpen_per_week_2019 AS l
ON
c.state = l.state
AND c.epidemiological_week = l.epidemiological_week;
DROP VIEW IF EXISTS deaths_brazil_by_arpen_per_week;
CREATE VIEW deaths_brazil_by_arpen_per_week AS
SELECT
epidemiological_week,
SUM(new_deaths_respiratory_failure_2020) AS new_deaths_respiratory_failure_2020,
SUM(deaths_respiratory_failure_2020) AS deaths_respiratory_failure_2020,
SUM(new_deaths_respiratory_failure_2019) AS new_deaths_respiratory_failure_2019,
SUM(deaths_respiratory_failure_2019) AS deaths_respiratory_failure_2019,
SUM(new_deaths_pneumonia_2020) AS new_deaths_pneumonia_2020,
SUM(deaths_pneumonia_2020) AS deaths_pneumonia_2020,
SUM(new_deaths_pneumonia_2019) AS new_deaths_pneumonia_2019,
SUM(deaths_pneumonia_2019) AS deaths_pneumonia_2019,
SUM(new_deaths_covid19_2020) AS new_deaths_covid19_2020,
SUM(deaths_covid19_2020) AS deaths_covid19_2020,
SUM(new_deaths_covid19_2019) AS new_deaths_covid19_2019,
SUM(deaths_covid19_2019) AS deaths_covid19_2019,
SUM(new_deaths_sars_2020) AS new_deaths_sars_2020,
SUM(deaths_sars_2020) AS deaths_sars_2020,
SUM(new_deaths_sars_2019) AS new_deaths_sars_2019,
SUM(deaths_sars_2019) AS deaths_sars_2019,
SUM(new_deaths_septicemia_2020) AS new_deaths_septicemia_2020,
SUM(deaths_septicemia_2020) AS deaths_septicemia_2020,
SUM(new_deaths_septicemia_2019) AS new_deaths_septicemia_2019,
SUM(deaths_septicemia_2019) AS deaths_septicemia_2019,
SUM(new_deaths_indeterminate_2020) AS new_deaths_indeterminate_2020,
SUM(deaths_indeterminate_2020) AS deaths_indeterminate_2020,
SUM(new_deaths_indeterminate_2019) AS new_deaths_indeterminate_2019,
SUM(deaths_indeterminate_2019) AS deaths_indeterminate_2019,
SUM(new_deaths_others_2020) AS new_deaths_others_2020,
SUM(deaths_others_2020) AS deaths_others_2020,
SUM(new_deaths_others_2019) AS new_deaths_others_2019,
SUM(deaths_others_2019) AS deaths_others_2019,
SUM(new_deaths_total_2020) AS new_deaths_total_2020,
SUM(deaths_total_2020) AS deaths_total_2020,
SUM(new_deaths_total_2019) AS new_deaths_total_2019,
SUM(deaths_total_2019) AS deaths_total_2019
FROM deaths_by_arpen_per_week
GROUP BY
epidemiological_week;
DROP VIEW IF EXISTS deaths_by_ses_per_week;
CREATE VIEW deaths_by_ses_per_week AS
SELECT
w.epidemiological_week,
c.state,
CASE WHEN MAX(c.confirmed) = '' THEN 0 ELSE MAX(c.confirmed) END AS confirmed,
CASE WHEN MAX(c.deaths) = '' THEN 0 ELSE MAX(c.deaths) END AS deaths
FROM caso AS c
LEFT JOIN epidemiological_week AS w
ON c.date = w.date
WHERE
c.place_type = 'state'
GROUP BY
w.epidemiological_week,
c.state
ORDER BY
w.epidemiological_week ASC,
c.state ASC;
DROP VIEW IF EXISTS deaths_brazil_by_ses_per_day;
CREATE VIEW deaths_brazil_by_ses_per_day AS
SELECT
c.date,
SUM(c.confirmed) AS confirmed,
SUM(c.deaths) AS deaths
FROM caso AS c
WHERE
c.place_type = 'state'
GROUP BY
c.date
ORDER BY
c.date;