Repository navigation
Expand file tree
/
Copy pathCovid Data Analysis.sql
More file actions
240 lines (181 loc) · 10.7 KB
/
Copy pathCovid Data Analysis.sql
File metadata and controls
240 lines (181 loc) · 10.7 KB
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
select *
from portfolioproject..coviddeaths
order by 2,3,4
select *
from portfolioproject..CovidVaccinations
delete
from portfolioproject..coviddeaths
where date = '2020-01-01 00:00:00.000' or date = '2020-01-02 00:00:00.000'
---------------------------------------------------------------------------------------------------------------------------------
--show chances getting infected with covid in any country (Infection Rate) datewise
---------------------------------------------------------------------------------------------------------------------------------
select location, date,population, total_cases,cast(total_cases as decimal)/cast(population as decimal)*100 as '%PopulationInfected'
from portfolioproject..coviddeaths
where continent is not null
--where location like '%india%'
order by 1,2
---------------------------------------------------------------------------------------------------------------------------------
--show chances getting infected with covid in any country (Infection Rate) overall
---------------------------------------------------------------------------------------------------------------------------------
select location,population, max(cast(total_cases as decimal)) as MaxTotalCases,max(cast(total_cases as decimal))/cast(population as decimal)*100 as '%PopulationInfected'
from portfolioproject..coviddeaths
where continent is not null
--where location like '%india%'
group by location,population
order by 4 desc
---------------------------------------------------------------------------------------------------------------------------------
--show chances of dying if you get infected with covid in any country datewise
---------------------------------------------------------------------------------------------------------------------------------
select location, date,population, total_cases,total_deaths, cast(total_deaths as decimal)/cast(total_cases as decimal)*100 as '%DeathsWithCases'
from portfolioproject..coviddeaths
where continent is not null
--where location like '%india%'
order by 1,2
---------------------------------------------------------------------------------------------------------------------------------
--show chances of dying with covid in any country wrt Population
---------------------------------------------------------------------------------------------------------------------------------
select location as Country,Max(Population) as Population,
max(cast(total_deaths as int)) as MaxTotalDeaths,
max(cast(total_deaths as int))/Max(Population) *100 as '%PopulationDeaths'
from portfolioproject..coviddeaths
where continent is not null
group by location
order by 4 desc
---------------------------------------------------------------------------------------------------------------------------------
--GLOBAL DATAS
---------------------------------------------------------------------------------------------------------------------------------
select dea.location as continent,
cast(population as decimal) Population,
max(cast(total_cases as decimal)) as TotalCases,
max(cast(total_cases as decimal))/cast(population as decimal)*100 as'%PopulationInfected',
max(cast(total_deaths as decimal)) as TotalDeaths,
max(cast(total_deaths as decimal))/cast(population as decimal)*100 as'%PopulationDeaths',
max(cast(people_vaccinated as decimal)) as PeopleVaccinated,
max(cast(people_vaccinated as decimal))/cast(population as decimal)*100 as'%PopulationVaccinated'
from portfolioproject..coviddeaths dea
join PortfolioProject..CovidVaccinations vac
on dea.location=vac.location and
dea.date=vac.date
where dea.continent is null
and
dea.location not like '%income%'and
dea.location not like'%world%'and
dea.location not like '%union%'
group by dea.location,dea.population
order by 8
---------------------------------------------------------------------------------------------------------------------------------
--show chances of dying with covid on Earth datewise by using given world data
---------------------------------------------------------------------------------------------------------------------------------
select date,max(population) as GlobalPopulation,
max(cast(total_cases as int)) GlobalTotalCases,
max(cast(total_cases as decimal))/cast(max(population) as decimal)*100 as '%PopulationInfected',
max(cast(total_deaths as int)) GlobalTotalDeaths,
max(cast(total_deaths as decimal))/cast(max(population) as decimal)*100 as '%PopulationDeaths',
max(cast(total_deaths as decimal))/max(cast(total_cases as decimal))*100 as '%CasesDeaths'
from portfolioproject..coviddeaths
where location like '%world%'
group by date
order by 1
---------------------------------------------------------------------------------------------------------------------------------
----show chances of dying with covid on Earth datewise without using given world data
---------------------------------------------------------------------------------------------------------------------------------
select date, sum(cast(population as bigint)) as GlobalPopulation,
sum(cast(total_cases as int)) as GlobalTotalCases,
sum(cast(total_cases as int))/cast((sum(cast(population as bigint ))) as decimal)*100 as '%PopulationInfected',
sum(cast(total_deaths as int)) GlobalTotalDeaths,
sum(cast(total_deaths as int))/cast((sum(cast(population as bigint ))) as decimal)*100 as '%PopulationDeaths',
sum(cast(total_deaths as int))/sum(cast(total_cases as decimal))*100 as '%CasesDeaths'
from portfolioproject..coviddeaths
where continent is not null
group by date
order by 1
---------------------------------------------------------------------------------------------------------------------------------
--Showing % of people vaccinated of any country datewise
---------------------------------------------------------------------------------------------------------------------------------
select dea.continent, dea.location, dea.date,
population,new_vaccinations_smoothed,
Sum(convert(bigint,vac.new_vaccinations_smoothed)) over (partition by dea.location order by dea.location, dea.date) as CumVaccinations,
people_vaccinated/population*100 '%PeopleVaccinated'
from PortfolioProject..CovidDeaths dea
join portfolioproject..CovidVaccinations vac
on dea.location =vac.location and
dea.date = vac.date
where dea.continent is not null --and dea.location like '%india%'
order by 2,3
---------------------------------------------------------------------------------------------------------------------------------
--Showing % of people vaccinated by country
---------------------------------------------------------------------------------------------------------------------------------
select dea.location,
case when (max(cast(population as bigint))) < (max(cast(people_vaccinated as bigint)))
then max(cast(people_vaccinated as bigint))
else max(cast(population as bigint))
end as population,
sum(cast(new_vaccinations_smoothed as bigint)) TotalVaccination,
max(cast(people_vaccinated as bigint)) PeopleVaccinated,
cast((max(cast(people_vaccinated as bigint))) as decimal)/cast(case when (max(cast(population as bigint))) < (max(cast(people_vaccinated as bigint)))
then max(cast(people_vaccinated as bigint))
else max(cast(population as bigint))
end as decimal)*100 '%PeopleVaccinated'
from PortfolioProject..CovidDeaths dea
join portfolioproject..CovidVaccinations vac
on dea.location =vac.location and
dea.date = vac.date
where dea.continent is not null --and dea.location like '%india%'
group by dea.location
order by 5 desc
---------------------------------------------------------------------------------------------------------------------------------
--Showing % of people vaccinated by country with CTE
---------------------------------------------------------------------------------------------------------------------------------
with PopVsVacc as
(select dea.continent, dea.location, dea.date,
population,new_vaccinations_smoothed,
people_vaccinated,
Sum(convert(bigint,vac.new_vaccinations_smoothed)) over (partition by dea.location order by dea.location, dea.date) as CumVaccinations
from PortfolioProject..CovidDeaths dea
join portfolioproject..CovidVaccinations vac
on dea.location =vac.location and
dea.date = vac.date
where dea.continent is not null --and dea.location like '%india%'
)
select*, people_vaccinated/population*100 '%PeopleVaccinated'
from Popvsvacc order by 2,3
---------------------------------------------------------------------------------------------------------------------------------
--Showing % of people vaccinated by country with TEMP table
---------------------------------------------------------------------------------------------------------------------------------
drop table if exists #PercentPeopleVaccinated
create table #PercentPeopleVaccinated
(continent nvarchar(255),
location nvarchar(255),
date datetime,
population numeric,
new_vaccinations_smoothed numeric,
people_vaccinated numeric,
cumvaccinations numeric)
insert into #PercentPeopleVaccinated
select dea.continent, dea.location, dea.date,
dea.population,new_vaccinations_smoothed,
people_vaccinated,
Sum(convert(bigint,vac.new_vaccinations_smoothed)) over (partition by dea.location order by dea.location, dea.date) as CumVaccinations
from PortfolioProject..CovidDeaths dea
join portfolioproject..CovidVaccinations vac
on dea.location =vac.location and
dea.date = vac.date
where dea.continent is not null --and dea.location like '%india%'
select *, people_vaccinated/population*100 as'%PeopleVaccinated'
from #PercentPeopleVaccinated order by 2,3
---------------------------------------------------------------------------------------------------------------------------------
--Creating a view to store data for later visualizations
---------------------------------------------------------------------------------------------------------------------------------
create view PercentPeopleVaccinated as
select dea.continent, dea.location, dea.date,
population,new_vaccinations_smoothed,
Sum(convert(bigint,vac.new_vaccinations_smoothed)) over (partition by dea.location order by dea.location, dea.date) as CumVaccinations,
people_vaccinated,
people_vaccinated/population*100 '%PeopleVaccinated'
from PortfolioProject..CovidDeaths dea
join portfolioproject..CovidVaccinations vac
on dea.location =vac.location and
dea.date = vac.date
where dea.continent is not null --and dea.location like '%india%'
select *
from PercentPeopleVaccinated