-
Notifications
You must be signed in to change notification settings - Fork 0
/
CovidDeathQueries.sql
174 lines (155 loc) · 5.33 KB
/
CovidDeathQueries.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
select * --location,date,total_cases, new_cases,total_deaths ,population
from PortfolioProject..covid_deaths$
WHERE continent is not null
ORDER BY 1,2
/*
SELECT location,
CASE
WHEN total_cases IS NOT NULL THEN
SUM(CAST (total_cases as INT)) /*,SUM(total_deaths)as total_deaths_in_country */
END total_cases_in_country
from PortfolioProject..covid_deaths$
GROUP BY location
ORDER BY 1
*/
alter table PortfolioProject..covid_deaths$ alter column total_cases numeric
alter table PortfolioProject..covid_deaths$ alter column total_deaths numeric
-- likelihood of dying
select location,date,total_cases,total_deaths,(total_deaths/total_cases) as death_ratio
from PortfolioProject..covid_deaths$
where location like '%unisia%'and continent is not null
ORDER BY 1,2
-- highest infection percentage
select
location,
MAX(total_cases) as total_cases,
MAX((total_cases/population))*100 as infection_percentage
from PortfolioProject..covid_deaths$
WHERE continent is not null
GROUP BY location
ORDER BY infection_percentage desc
-- highest death count per population
select
location,
population,
MAX(total_deaths) as total_deaths,
MAX((total_deaths/population))*100 as death_per_population
from PortfolioProject..covid_deaths$
WHERE continent is not null
GROUP BY location,population
ORDER BY death_per_population desc
-- highest death count per population group by continent (RESULTS ARE WRONG HERE -> CHECK IT LATER!!!)
select
continent,
MAX(total_deaths) as total_deaths,
MAX((total_deaths/population))*100 as death_per_population
from PortfolioProject..covid_deaths$
WHERE continent is not null
GROUP BY continent
ORDER BY death_per_population desc
-- highest death count per population group by continent (RIGHT WAY)
select
location,
MAX(total_deaths) as total_deaths,
MAX((total_deaths/population))*100 as death_per_population
from PortfolioProject..covid_deaths$
WHERE continent is null -- FITER ONLY ROWS CONCERNING CONTINENT
GROUP BY location
ORDER BY death_per_population desc
---- highest death count in united states
select
location,
population,
MAX(total_deaths) as total_deaths,
MAX((total_deaths/population))*100 as death_per_population
from PortfolioProject..covid_deaths$
WHERE continent is not null and location like'%states%'
GROUP BY location,population
ORDER BY death_per_population desc
-- global figures
---- comparing new deaths and new cases per day
select date
, SUM(new_cases) as new_cases
, SUM(cast(new_deaths as int))as new_deaths
, SUM(cast(new_deaths as int))/SUM(new_cases)*100 as death_per_case_percentage
from PortfolioProject..covid_deaths$
WHERE continent is not null and new_cases> 0
GROUP BY date
ORDER BY 1
alter table PortfolioProject..covid_vaccinations$ alter column new_vaccinations numeric
alter table PortfolioProject..covid_vaccinations$ alter column total_vaccinations numeric
-- daily Vaccination rate
select
dea.continent,
dea.location,
dea.date,
new_vaccinations,
total_vaccinations,
--population,
new_vaccinations/total_vaccinations*100 as vaccinated_percenatge
from PortfolioProject..covid_deaths$ dea
Join PortfolioProject..covid_vaccinations$ vac
ON dea.location = vac.location
and dea.date= vac.date
WHERE total_vaccinations>0 and dea.continent is not null
ORDER BY 1,2,3
--- total population vs vaccination
-- USE CTE for total population vs vaccination
With POPvsVAC (continent,location,date,population,new_vaccinations,rolling_people_vaccinated)
as(
select
dea.continent,
dea.location,
dea.date,
population,
new_vaccinations,
SUM(new_vaccinations/population) OVER (Partition by dea.location,dea.date) as rolling_vaccinated_people
from PortfolioProject..covid_deaths$ dea
Join PortfolioProject..covid_vaccinations$ vac
ON dea.location = vac.location
and dea.date= vac.date
WHERE total_vaccinations>0 and dea.continent is not null
)
select* ,(rolling_people_vaccinated/population)*100 as vacination_percentage
from POPvsVAC
-- USE TEMP TABLE for total population vs vaccination
DROP TABLE if exists #tempPOPvsVAC
CREATE TABLE #tempPOPvsVAC
(
continent nvarchar(225),
location nvarchar(225),
date datetime,
population numeric,
new_vaccinations numeric ,
rolling_people_vaccinated numeric
)
Insert into #tempPOPvsVAC
select
dea.continent,
dea.location,
dea.date,
population,
new_vaccinations,
SUM(new_vaccinations/population) OVER (Partition by dea.location,dea.date) as rolling_vaccinated_people
from PortfolioProject..covid_deaths$ dea
Join PortfolioProject..covid_vaccinations$ vac
ON dea.location = vac.location
and dea.date= vac.date
WHERE total_vaccinations>0 and dea.continent is not null
select* ,(rolling_people_vaccinated/population)*100 as vacination_percentage
from #tempPOPvsVAC
--Create View for later visualization in Tableau
CREATE View VaccinatedPopulationPercentage as
select
dea.continent,
dea.location,
dea.date,
population,
new_vaccinations,
SUM(new_vaccinations/population) OVER (Partition by dea.location,dea.date) as rolling_vaccinated_people
from PortfolioProject..covid_deaths$ dea
Join PortfolioProject..covid_vaccinations$ vac
ON dea.location = vac.location
and dea.date= vac.date
WHERE total_vaccinations>0 and dea.continent is not null
select * from VaccinatedPopulationPercentage