-
Notifications
You must be signed in to change notification settings - Fork 0
/
Covid_Project.sql
547 lines (409 loc) · 15.1 KB
/
Covid_Project.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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
-- Showing the data from (CovidDeaths) table
Select *
FROM Covid..CovidDeaths$
WHERE continent IS NOT NULL
ORDER BY location;
-- Showing the data from (CovidVaccinations) table
Select *
FROM Covid..CovidVaccinations$
WHERE continent IS NOT NULL
ORDER BY location;
-- Total cases VS Total deaths & The percentage between them
-- Here is the The percentage of Total deaths to Total cases in Egypt Per day
SELECT location,
CONVERT(date,date) AS Date,
total_cases,
total_deaths,
ROUND((total_deaths/total_cases)*100,2) AS The_percentage_of_death
From Covid..CovidDeaths$
WHERE location = 'Egypt'
AND continent IS NOT NULL;
-- Total cases VS Total deaths & The percentage between them
-- Here is the The percentage of Total deaths to Total cases in Asia Per day
SELECT location,
CONVERT(date,date) AS Date,
total_cases,
total_deaths,
ROUND((total_deaths/total_cases)*100,2) AS The_percentage_of_death
From Covid..CovidDeaths$
WHERE location = 'Africa'
AND continent IS NULL;
-- Total Cases VS Population & The percentage between them
-- Here is the The percentage of infected people in united states
SELECT location,
CONVERT(date,date) AS Date,
total_cases,
population,
ROUND((total_cases/population)*100,2) AS The_percentage_of_infected_people
From Covid..CovidDeaths$
WHERE location = 'united states'
AND continent IS NOT NULL
ORDER BY Date;
-- Total Cases VS Population & The percentage between them
-- Here is the The percentage of infected people in Europe
SELECT location,
CONVERT(date,date) AS Date,
total_cases,
population,
ROUND((total_cases/population)*100,2) AS The_percentage_of_infected_people
From Covid..CovidDeaths$
WHERE location = 'Europe'
AND continent IS NULL
ORDER BY Date;
-- Total Deaths VS Population & The percentage between them
-- Here is the The percentage of Deaths people in united states
SELECT location,
CONVERT(date,date) AS Date,
total_deaths,
population,
ROUND((total_deaths/population)*100,4) AS The_percentage_of_deaths_people
From Covid..CovidDeaths$
WHERE location = 'united states'
AND continent IS NOT NULL
ORDER BY Date;
-- Total Deaths VS Population & The percentage between them
-- Here is the The percentage of Deaths people in Europe
SELECT location,
CONVERT(date,date) AS Date,
total_deaths,
population,
ROUND((total_deaths/population)*100,4) AS The_percentage_of_deaths_people
From Covid..CovidDeaths$
WHERE location = 'Europe'
AND continent IS NULL
ORDER BY Date;
-- Top 10 Countries by Total Cases
SELECT TOP 10 cd.location,
SUM(CONVERT(NUMERIC, total_cases)) AS Total_Cases
FROM Covid..CovidDeaths$ cd
WHERE cd.continent IS NOT NULL
GROUP BY cd.location
ORDER BY Total_Cases DESC;
-- Top 10 Countries by Total Deaths
SELECT TOP 10 cd.location,
SUM(CONVERT(NUMERIC, total_deaths)) AS Total_Deaths
FROM Covid..CovidDeaths$ cd
WHERE cd.continent IS NOT NULL
GROUP BY cd.location
ORDER BY Total_Deaths DESC;
-- Maximum number of cases of each country
SELECT location,
MAX(CAST(total_cases AS INT)) AS Total_Cases
From Covid..CovidDeaths$
WHERE continent IS NOT NULL
GROUP BY location
ORDER BY Total_Cases DESC;
-- Maximum number of deaths of each country
SELECT location,
MAX(CAST(total_deaths AS INT)) AS Total_Deaths
From Covid..CovidDeaths$
WHERE continent IS NOT NULL
GROUP BY location
ORDER BY Total_Deaths DESC;
-- Combined Total Cases and Deaths by Country
SELECT location, SUM(CAST(total_cases AS NUMERIC)) AS Total_Count, 'Cases' AS Type
FROM Covid..CovidDeaths$
WHERE continent IS NOT NULL
GROUP BY location
UNION
SELECT location, SUM(CAST(total_deaths AS NUMERIC)) AS Total_Count, 'Deaths' AS Type
FROM Covid..CovidDeaths$
WHERE continent IS NOT NULL
GROUP BY location
ORDER BY location, Type;
-- Maximum infected Percentage of each Country
SELECT location,
population,
MAX(total_cases) AS maximum_infected_Cases,
ROUND(MAX((total_cases/population))*100,3) AS maximum_infected_percentage_of_each_country
From Covid..CovidDeaths$
WHERE continent IS NOT NULL
GROUP BY location , population
ORDER BY maximum_infected_percentage_of_each_country DESC;
-- Maximum number of deaths of each continent
SELECT location,
MAX(CAST(total_deaths AS NUMERIC)) AS Total_Deaths
From Covid..CovidDeaths$
WHERE continent IS NULL
GROUP BY location
ORDER BY Total_Deaths DESC;
-- Calculating the percentage of deaths per day
SELECT
CONVERT(date,date) AS Date,
SUM(new_cases) AS Total_Cases,
SUM(CAST(new_deaths AS INT)) AS Total_Deaths,
CASE
WHEN SUM(new_cases) = 0 THEN 0
ELSE ROUND(SUM(CAST(new_deaths AS INT)) / SUM(new_cases) * 100,2)
END AS Death_Percentage
FROM Covid..CovidDeaths$
WHERE continent IS NOT NULL
GROUP BY Date
ORDER BY Date;
-- Calculating the percentage of deaths of all time
SELECT
SUM(new_cases) AS Total_Cases,
SUM(CAST(new_deaths AS INT)) AS Total_Deaths,
CASE
WHEN SUM(new_cases) = 0 THEN 0
ELSE ROUND(SUM(CAST(new_deaths AS INT)) / SUM(new_cases) * 100,2)
END AS Death_Percentage
FROM Covid..CovidDeaths$
WHERE continent IS NOT NULL;
-- Daily Average New Cases and Deaths for a Specific Continent (Asia)
SELECT location,
CONVERT(date, date) AS Date,
ROUND(AVG(CAST(new_cases AS INT)),5) AS Average_Daily_New_Cases,
ROUND(AVG(CAST(new_deaths AS INT)),5) AS Average_Daily_New_Deaths
FROM Covid..CovidDeaths$
WHERE location = 'Asia'
AND continent IS NULL
GROUP BY location, Date
ORDER BY Date;
-- Cumulative Cases and Deaths for the Top 5 Most Populated Countries
WITH Top_Populated_Countries AS (
SELECT TOP 5 location
FROM Covid..CovidDeaths$
WHERE continent IS NOT NULL
GROUP BY location
ORDER BY MAX(population) DESC
)
SELECT location,
date,
SUM(CAST(new_cases AS NUMERIC)) OVER (PARTITION BY location ORDER BY date) AS Cumulative_Cases,
SUM(CAST(new_deaths AS NUMERIC)) OVER (PARTITION BY location ORDER BY date) AS Cumulative_Deaths
FROM Covid..CovidDeaths$
WHERE location IN (SELECT location FROM Top_Populated_Countries)
ORDER BY location, date;
-- Increasing in Daily New Cases for specific country
SELECT a.location,
CONVERT(date,a.date) AS Date,
a.new_cases AS Current_Day_Cases,
b.new_cases AS Previous_Day_Cases,
a.new_cases - b.new_cases AS increase_OR_decrease
FROM Covid..CovidDeaths$ a
JOIN Covid..CovidDeaths$ b
ON a.location = b.location
AND a.date = DATEADD(day, 1, b.date)
WHERE a.continent IS NOT NULL
AND a.location = 'Egypt'
ORDER BY a.location, a.date;
-- OR --
SELECT location,
CONVERT(date, date) AS Date,
new_cases AS Current_Day_Cases,
LAG(new_cases) OVER (ORDER BY date) AS Previous_Day_Cases,
new_cases - LAG(new_cases) OVER (ORDER BY date) AS increase_OR_decrease
FROM Covid..CovidDeaths$
WHERE continent IS NOT NULL
AND location = 'Egypt'
ORDER BY location, date;
-- Showing the new vaccinations per day for each country
SELECT cd.location,
format(cd.date,'dd-MM-yyyy') AS Date,
cv.new_vaccinations
FROM Covid..CovidDeaths$ cd INNER JOIN Covid..CovidVaccinations$ cv
ON cd.location = cv.location
AND cd.date = cv.date
WHERE cd.continent IS NOT NULL
ORDER BY cd.location , Date;
-- Showing the new vaccinations and Cumulative Total vaccinations for each country
SELECT cd.location,
FORMAT(cd.date,'dd-MM-yyyy') AS Date,
cv.new_vaccinations,
SUM(CONVERT(INT,cv.new_vaccinations)) OVER (PARTITION BY cd.location ORDER BY cd.location , cd.date) AS Total_vaccinations
FROM Covid..CovidDeaths$ cd INNER JOIN Covid..CovidVaccinations$ cv
ON cd.location = cv.location
AND cd.date = cv.date
WHERE cd.continent IS NOT NULL;
-- Creating View Showing the Total vaccinations for each country
CREATE VIEW Total_Vaccinations_of_each_country
AS
SELECT cd.location,
SUM(CAST(cv.new_vaccinations AS NUMERIC)) AS Total_Vaccinations
FROM Covid..CovidDeaths$ cd INNER JOIN Covid..CovidVaccinations$ cv
ON cd.location = cv.location
WHERE cd.continent IS NOT NULL
GROUP BY cd.location;
-- using that view --
SELECT *
FROM Total_Vaccinations_of_each_country
ORDER BY Total_Vaccinations DESC;
-- Showing the Total vaccinations for each Continent
SELECT cd.location,
SUM(CAST(cv.new_vaccinations AS NUMERIC)) AS Total_Vaccinations
FROM Covid..CovidDeaths$ cd INNER JOIN Covid..CovidVaccinations$ cv
ON cd.location = cv.location
WHERE cd.continent IS NULL
GROUP BY cd.location
ORDER BY Total_Vaccinations DESC;
-- Top 10 Countries by Total Vaccinations
SELECT TOP 10 cd.location,
SUM(CONVERT(NUMERIC, cv.new_vaccinations)) AS Total_Vaccinations
FROM Covid..CovidDeaths$ cd INNER JOIN Covid..CovidVaccinations$ cv
ON cd.location = cv.location
WHERE cd.continent IS NOT NULL
GROUP BY cd.location
ORDER BY Total_Vaccinations DESC;
-- Top 2 Continents by Total Vaccinations
SELECT TOP 3 cd.location,
SUM(CONVERT(NUMERIC, cv.new_vaccinations)) AS Total_Vaccinations
FROM Covid..CovidDeaths$ cd INNER JOIN Covid..CovidVaccinations$ cv
ON cd.location = cv.location
WHERE cd.continent IS NULL
GROUP BY cd.location
ORDER BY Total_Vaccinations DESC;
-- Total Cases, Deaths, and Vaccinations by Month for a Specific Country
SELECT cd.location,
YEAR(cd.date) AS Year,
MONTH(cd.date) AS Month,
SUM(cd.total_cases) AS Total_Cases,
SUM(CAST(cd.total_deaths AS NUMERIC)) AS Total_Deaths,
SUM(CAST(cv.new_vaccinations AS NUMERIC)) AS Total_Vaccinations
FROM Covid..CovidDeaths$ cd
JOIN Covid..CovidVaccinations$ cv
ON cd.location = cv.location
AND cd.date = cv.date
WHERE cd.location = 'Russia'
AND cd.continent IS NOT NULL
GROUP BY cd.location, YEAR(cd.date), MONTH(cd.date)
ORDER BY Year, Month;
-- Countries with Highest Vaccination Rates Relative to Population
SELECT TOP 10 cd.location,
cd.population,
SUM(CAST(cv.new_vaccinations AS NUMERIC)) AS Total_Vaccinations,
ROUND((SUM(CAST(cv.new_vaccinations AS NUMERIC)) / cd.population),4) * 100 AS Vaccination_Rate
FROM Covid..CovidDeaths$ cd INNER JOIN Covid..CovidVaccinations$ cv
ON cd.location = cv.location
AND cd.date = cv.date
WHERE cd.continent IS NOT NULL
GROUP BY cd.location, cd.population
ORDER BY Vaccination_Rate DESC;
-- Total Vaccinations vs. Total Deaths by Continent
SELECT cd.location,
SUM(CAST(cv.new_vaccinations AS NUMERIC)) AS Total_Vaccinations,
SUM(CAST(cd.total_deaths AS NUMERIC)) AS Total_Deaths
FROM Covid..CovidDeaths$ cd INNER JOIN Covid..CovidVaccinations$ cv
ON cd.location = cv.location
AND cd.date = cv.date
WHERE cd.continent IS NULL
GROUP BY cd.location
ORDER BY cd.location;
-- Vaccination Rates and Case Fatality Rate by Country
SELECT cd.location,
cd.population,
SUM(CAST(cv.new_vaccinations AS NUMERIC)) AS Total_Vaccinations,
(SUM(CAST(cv.new_vaccinations AS NUMERIC)) / cd.population) * 100 AS Vaccination_Rate,
SUM(CAST(cd.total_deaths AS NUMERIC)) AS Total_Deaths,
SUM(CAST(cd.total_cases AS NUMERIC)) AS Total_Cases,
(SUM(CAST(cd.total_deaths AS NUMERIC)) / SUM(CAST(cd.total_cases AS NUMERIC))) * 100 AS Case_Fatality_Rate
FROM Covid..CovidDeaths$ cd INNER JOIN Covid..CovidVaccinations$ cv
ON cd.location = cv.location
AND cd.date = cv.date
WHERE cd.continent IS NOT NULL
GROUP BY cd.location, cd.population
ORDER BY Vaccination_Rate DESC;
-- Creating Temp Table (#percentage_of_population_vaccinated) for each continent
DROP TABLE IF EXISTS #percentage_of_population_vaccinated
CREATE TABLE #percentage_of_population_vaccinated
(
continent nvarchar(255),
location nvarchar(255),
date datetime,
population numeric,
new_vaccinations numeric,
Total_vaccinations_per_country numeric
)
INSERT INTO #percentage_of_population_vaccinated
SELECT cd.continent,
cd.location,
cd.date,
cd.population,
cv.new_vaccinations,
SUM(CONVERT(INT,cv.new_vaccinations)) OVER (PARTITION BY cd.location ORDER BY cd.location , cd.date) AS Total_vaccinations_per_continent
FROM Covid..CovidDeaths$ cd INNER JOIN Covid..CovidVaccinations$ cv
ON cd.location = cv.location
AND cd.date = cv.date
WHERE cd.continent IS NULL;
-- Use the Temp Table
Select *, ROUND((Total_vaccinations_per_country / population),4) * 100 AS Total_vaccinations_per_continent_percentage
FROM #percentage_of_population_vaccinated;
-- Create an Index on Location and Date
CREATE INDEX idx_location_date ON Covid..CovidDeaths$ (location, date);
-- Create function calculates the total number of vaccinations for a specified location. (Scalar Fun.)
CREATE FUNCTION get_tot_Vac (@location NVARCHAR(255))
RETURNS NUMERIC
AS
BEGIN
DECLARE @TotalVaccinations NUMERIC;
SELECT @TotalVaccinations = SUM(CONVERT(NUMERIC, cv.new_vaccinations))
FROM Covid..CovidDeaths$ cd
INNER JOIN Covid..CovidVaccinations$ cv ON cd.location = cv.location
WHERE cd.continent IS NOT NULL
AND cd.location = @location;
RETURN @TotalVaccinations;
END;
GO
SELECT dbo.get_tot_Vac('United States') AS Total_Vaccinations;
-- Create inline table-valued function that returns a table with specific columns related to COVID-19 data for a specified location. (inline Fun.)
CREATE FUNCTION Infected_Percentage(@location NVARCHAR(255))
RETURNS TABLE
AS
RETURN
(
SELECT location,
CONVERT(date, date) AS Date,
total_cases,
population,
ROUND((total_cases/population)*100, 2) AS The_percentage_of_infected_people
FROM Covid..CovidDeaths$
WHERE location = @location
AND continent IS NULL
);
GO
SELECT *
FROM Infected_Percentage('Europe');
-- Create a multi-statement table-valued function that retrieves COVID-19 vaccination data by location and date. (Multi-Statement Fun.)
CREATE FUNCTION Vaccinations()
RETURNS @Result TABLE
(
location NVARCHAR(255),
Date NVARCHAR(10),
new_vaccinations INT
)
AS
BEGIN
INSERT INTO @Result
SELECT cd.location,
FORMAT(cd.date, 'dd-MM-yyyy') AS Date,
cv.new_vaccinations
FROM Covid..CovidDeaths$ cd INNER JOIN Covid..CovidVaccinations$ cv
ON cd.location = cv.location
AND cd.date = cv.date
WHERE cd.continent IS NOT NULL
ORDER BY cd.location, Date;
RETURN;
END;
GO
SELECT *
FROM Vaccinations();
-- Create a Stored Procedure to Get COVID-19 Statistics for a Specific Country
CREATE PROCEDURE Get_Covid_Stats
@Country NVARCHAR(255)
AS
BEGIN
SELECT cd.location,
FORMAT(cd.date,'dd-MM-yyyy') AS Date,
cd.total_cases,
cd.total_deaths,
cv.total_vaccinations
FROM Covid..CovidDeaths$ cd INNER JOIN Covid..CovidVaccinations$ cv
ON cd.location = cv.location
AND cd.date = cv.date
WHERE cd.continent IS NOT NULL
AND cd.location = @Country
ORDER BY Date;
END;
GO
--Execute the Stored Procedure
EXEC Get_Covid_Stats @Country = 'Egypt';