-
Notifications
You must be signed in to change notification settings - Fork 0
/
COVID Portfolio Project v0.1.1.sql
292 lines (223 loc) · 8.41 KB
/
COVID Portfolio Project v0.1.1.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
290
291
292
--ALTER TABLE covidDeaths$
--ALTER COLUMN
-- total_deaths float;
--ALTER TABLE covidDeaths$
--ALTER COLUMN
-- total_cases float;
--ALTER TABLE covidDeaths$
--ALTER COLUMN
-- total_cases_per_million float;
--ALTER TABLE covidDeaths$
--ALTER COLUMN
-- total_deaths_per_million float;
--ALTER TABLE covidDeaths$
--ALTER COLUMN
-- reproduction_rate float;
--ALTER TABLE covidDeaths$
--ALTER COLUMN
-- icu_patients int;
--ALTER TABLE covidDeaths$
--ALTER COLUMN
-- icu_patients_per_million float;
--ALTER TABLE covidDeaths$
--ALTER COLUMN
-- hosp_patients int;
--ALTER TABLE covidDeaths$
--ALTER COLUMN
-- hosp_patients_per_million float;
--ALTER TABLE covidDeaths$
--ALTER COLUMN
-- weekly_icu_admissions int;
--ALTER TABLE covidDeaths$
--ALTER COLUMN
-- weekly_icu_admissions_per_million float;
--ALTER TABLE covidDeaths$
--ALTER COLUMN
-- weekly_hosp_admissions int;
--ALTER TABLE covidDeaths$
--ALTER COLUMN
-- weekly_hosp_admissions_per_million float;
SELECT TOP(5) * FROM covidDeaths$;
SELECT TOP(5) * FROM covidVaccinations$;
SELECT Location, date, total_cases, new_cases, total_deaths, population
FROM covidDeaths$
ORDER BY 1,2;
--Looking at Total Cases vs Total Deaths
--Infection-Death ration
SELECT Location, date, total_cases, total_deaths, CAST((total_deaths/total_cases)*100 AS DECIMAL(4,2)) AS "Infection/Death ratio"
FROM covidDeaths$
WHERE (total_deaths IS NOT NULL OR total_cases IS NOT NULL) AND location LIKE 'venezuela'
ORDER BY 1,2
--Cases per Population
SELECT Location, date, population, total_cases, CAST((total_cases/population)*100 AS DECIMAL(4,2)) AS "Infection/Population ratio"
FROM covidDeaths$
WHERE location LIKE '%states%'
ORDER BY 1,2
--Countries with highest Infection Rate compared to Population
SELECT Location, Population, MAX(total_cases) as 'highest infection count', MAX((total_cases/population))*100 AS 'infected population ratio'
FROM covidDeaths$
GROUP BY population, location
ORDER BY 4 DESC
--Showing Countries with Highest Death Count per Population
SELECT location, MAX(total_deaths) AS 'Death Count'
FROM covidDeaths$
--WHERE location NOT IN('World', 'High Income','Upper middle income','Lower middle income', 'European Union', 'Europe', 'North America', 'Asia', 'South America')
WHERE continent IS NOT NULL --This is much more efficient and user friendly than the above snippet.
GROUP BY location
ORDER BY 'Death Count' DESC
--LET'S BREAK THINGS DOWN BY CONTINENT
--Deaths by region
SELECT location, MAX(total_deaths) AS 'Death Count'
FROM covidDeaths$
WHERE continent IS NULL AND location not like '%income'
GROUP BY location
ORDER BY 'Death Count' DESC
-- LOOKING AT GLOBAL NUMBERS
SELECT /*date,*/ SUM(new_cases) AS Glb_newCases, SUM(new_deaths) AS Glb_newDeaths, (SUM(new_deaths)/SUM(new_cases))*100 AS 'Global Death ratio'
FROM covidDeaths$
WHERE continent IS NOT NULL
--GROUP BY date
ORDER BY 1,2
-- WORKING WITH THE VACCINATIONS DATA
--ALTER TABLE PortfolioProject..covidVaccinations$
--ALTER COLUMN
-- [new_tests] int;
--ALTER TABLE PortfolioProject..covidVaccinations$
--ALTER COLUMN
-- [total_tests_per_thousand] float;
--ALTER TABLE PortfolioProject..covidVaccinations$
--ALTER COLUMN
-- [new_tests_per_thousand] float;
--ALTER TABLE PortfolioProject..covidVaccinations$
--ALTER COLUMN
-- [new_tests_smoothed] float;
--ALTER TABLE PortfolioProject..covidVaccinations$
--ALTER COLUMN
-- [new_tests_smoothed_per_thousand] float;
--ALTER TABLE PortfolioProject..covidVaccinations$
--ALTER COLUMN
-- [positive_rate] float;
--ALTER TABLE PortfolioProject..covidVaccinations$
--ALTER COLUMN
-- [tests_per_case] float;
--ALTER TABLE PortfolioProject..covidVaccinations$
--ALTER COLUMN
-- [total_vaccinations] float;
--ALTER TABLE PortfolioProject..covidVaccinations$
--ALTER COLUMN
-- [people_vaccinated] float;
--ALTER TABLE PortfolioProject..covidVaccinations$
--ALTER COLUMN
-- [people_fully_vaccinated] float;
--ALTER TABLE PortfolioProject..covidVaccinations$
--ALTER COLUMN
-- [total_boosters] float;
--ALTER TABLE PortfolioProject..covidVaccinations$
--ALTER COLUMN
-- [new_vaccinations] float;
--ALTER TABLE PortfolioProject..covidVaccinations$
--ALTER COLUMN
-- [new_vaccinations_smoothed] float;
--ALTER TABLE PortfolioProject..covidVaccinations$
--ALTER COLUMN
-- [total_vaccinations_per_hundred] float;
--ALTER TABLE PortfolioProject..covidVaccinations$
--ALTER COLUMN
-- [people_vaccinated_per_hundred] float;
--ALTER TABLE PortfolioProject..covidVaccinations$
--ALTER COLUMN
-- [people_fully_vaccinated_per_hundred] float;
--ALTER TABLE PortfolioProject..covidVaccinations$
--ALTER COLUMN
-- [total_boosters_per_hundred] float;
--ALTER TABLE PortfolioProject..covidVaccinations$
--ALTER COLUMN
-- [new_vaccinations_smoothed_per_million] float;
--ALTER TABLE PortfolioProject..covidVaccinations$
--ALTER COLUMN
-- [new_people_vaccinated_smoothed] float;
--ALTER TABLE PortfolioProject..covidVaccinations$
--ALTER COLUMN
-- [new_people_vaccinated_smoothed_per_hundred] float;
--ALTER TABLE PortfolioProject..covidVaccinations$
--ALTER COLUMN
-- [extreme_poverty] float;
--ALTER TABLE PortfolioProject..covidVaccinations$
--ALTER COLUMN
-- [excess_mortality_cumulative_absolute] float;
--ALTER TABLE PortfolioProject..covidVaccinations$
--ALTER COLUMN
-- [male_smokers] float;
--ALTER TABLE PortfolioProject..covidVaccinations$
--ALTER COLUMN
-- [female_smokers] float;
--ALTER TABLE PortfolioProject..covidVaccinations$
--ALTER COLUMN
-- [excess_mortality_cumulative] float;
--ALTER TABLE PortfolioProject..covidVaccinations$
--ALTER COLUMN
-- [excess_mortality] float;
--ALTER TABLE PortfolioProject..covidVaccinations$
--ALTER COLUMN
-- [excess_mortality_cumulative_per_million] float;
-- JOINING THE TWO DATASETS
--Looking at total population vaccinations
SELECT cD.continent, cD.location, cD.date, cD. population, cV.new_vaccinations
FROM covidDeaths$ cD
JOIN covidVaccinations$ cV
ON cD.date = cV.date AND cD.location = cV.location
WHERE cD.continent is not null
ORDER BY 1, 2, 3
--rolling count of new vaccinations
SELECT cD.continent, cD.location, cD.date, cD. population, cV.new_vaccinations, SUM(cV.new_vaccinations) OVER (PARTITION BY cD.location ORDER BY cD.location, cD.date) AS Rolling_Vaccinations
FROM covidDeaths$ cD
JOIN covidVaccinations$ cV
ON cD.date = cV.date
AND cD.location = cV.location
WHERE cD.continent is not null
ORDER BY 1, 2, 3
--Using a CTE to calculate a rolling percentage of population vaccination
With PopVsVac (continent, location, date, population, new_vaccinations, Rolling_Vaccinations)
AS (
SELECT cD.continent, cD.location, cD.date, cD. population, cV.new_vaccinations, SUM(cV.new_vaccinations) OVER (PARTITION BY cD.location ORDER BY cD.location, cD.date) AS Rolling_Vaccinations
FROM covidDeaths$ cD
JOIN covidVaccinations$ cV
ON cD.date = cV.date
AND cD.location = cV.location
WHERE cD.continent is not null
--ORDER BY 2, 3
)
SELECT *, CAST((Rolling_Vaccinations/population)*100 AS decimal(5,2)) AS 'Vaccinated_Pop_%'
FROM PopVsVac
ORDER BY 1,2,3
-- TEMP TABLE
DROP TABLE IF EXISTS #VaccinatedPopulation
CREATE TABLE #VaccinatedPopulation
(
Continent nvarchar(255),
Location nvarchar(255),
Date datetime,
Population numeric,
New_vaccinations numeric,
RollingVaccinatedPopulation numeric
)
INSERT INTO #VaccinatedPopulation
SELECT cD.continent, cD.location, cD.date, cD. population, cV.new_vaccinations, SUM(cV.new_vaccinations) OVER (PARTITION BY cD.location ORDER BY cD.location, cD.date) AS RollingVaccinatedPopulation
FROM covidDeaths$ cD
JOIN covidVaccinations$ cV
ON cD.date = cV.date
AND cD.location = cV.location
WHERE cD.continent is not null
--ORDER BY 2, 3
SELECT *, CAST((RollingVaccinatedPopulation /population)*100 AS decimal(5,2)) AS 'Vaccinated_Pop_%'
FROM #VaccinatedPopulation
ORDER BY 1,2,3
--CREATING A VIEW FOR LATER VISUALIZATIONS
CREATE VIEW PercentPopulationVaccinated AS
SELECT cD.continent, cD.location, cD.date, cD. population, cV.new_vaccinations, SUM(cV.new_vaccinations) OVER (PARTITION BY cD.location ORDER BY cD.location, cD.date) AS Rolling_Vaccinations
FROM covidDeaths$ cD
JOIN covidVaccinations$ cV
ON cD.date = cV.date
AND cD.location = cV.location
WHERE cD.continent is not null
--ORDER BY 2, 3