-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCovid Exploration Project.sql
More file actions
207 lines (151 loc) · 6.75 KB
/
Copy pathCovid Exploration Project.sql
File metadata and controls
207 lines (151 loc) · 6.75 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
--Select Everything from CovidDeaths
Select * from PortfolioProject..CovidDeaths
Where continent is not NULL
Order by 3,4
--Select Everything From CovidVaccinations
Select * from PortfolioProject..CovidVaccinations
Where continent is NOT NULL
Order by 3,4
-- Selecting Data that we are using
Select location,date, total_cases, new_cases,total_deaths, population
From PortfolioProject..CovidDeaths
Where continent is NOT NULL
Order By 1,2
-- List the name of location, unique location
Select distinct(location) from PortfolioProject..CovidDeaths
Where continent is NOT NULL
Order by location
-- total_deaths VS total_case in my Continent and Country
Select location,date,population, total_cases,new_cases,total_deaths,new_deaths,round((total_cases/population)*100,3) As TotalInfectedPopulation,Round((total_deaths/total_cases)*100,3) As DeathPercentage
From PortfolioProject..CovidDeaths
Where location Like '%Nepal%'
--AND continent is NOT NULL
Order By 1,2
Select location,population, max(total_cases) As MaximumCases, (max(total_cases)/population)*100 AS InfectedPeople,
Max(cast(total_deaths as Int)) As TotalDeath, (max(cast(total_deaths as int)/total_cases))*100 As DeathCountPerCases
From PortfolioProject..CovidDeaths
--Where location in ('Nepal','India')
Where continent is not NUll
Group By location, population
-- total_deaths vs total_case in wordl
Select location, date, population, total_cases, total_deaths, (total_deaths/total_cases)*100 As DeathRateWorldWide
from PortfolioProject..CovidDeaths
Where location = 'World'
--Order by 3,4
-- Total Cases vs Population
Select Continent,location,date, total_cases, new_cases,population,Round((total_cases/population)*100,3) As InfectedPercentage
From PortfolioProject..CovidDeaths
--Where location in ('Asia','Nepal')
Where continent is NOT NULL
Order By 2,3
-- WorldWide cases
Select location, date, population, total_cases,new_cases, total_deaths, (total_cases/population)*100 As InfectedRateWorldWide
from PortfolioProject..CovidDeaths
Where location = 'World'
-- Highest Infection Rate
Select location,population, max(total_cases) as HighestInfectionCount,Max(Round((total_cases/population)*100,3)) As HighestInfectedPercentage
From PortfolioProject..CovidDeaths
--Where location in ('Asia','Nepal')
Where Continent is Not Null
Group By location, population
Order by HighestInfectedPercentage Desc
-- Highest Death Count per Population
-- Showing location with the highest death count per population
Select location, max(cast(total_deaths as int)) As TotalDeathCount
From PortfolioProject..CovidDeaths
Where continent is NOT NULL
--And location = 'Nepal'
Group By location
Order by TotalDeathCount Desc
-- Highest Death Count per Population
-- Showing Continent with the highest death count per population
Select location, max(cast(total_deaths as int)) As TotalDeathCount
From PortfolioProject..CovidDeaths
Where continent is NULL
Group By location
Order by TotalDeathCount Desc
-- or
Select continent, max(cast(total_deaths as int)) As TotalDeathCount
From PortfolioProject..CovidDeaths
Where continent is NOT NULL
Group By continent
Order by TotalDeathCount Desc
-- Global Numbers
-- Death Percentage Per Day globally
Select date,sum(new_cases) as total_cases,sum(cast(new_deaths as int)) as total_death,
Round(sum(cast(new_deaths as int))/sum(new_cases)*100,3) as DeathPercentageGlobal
From PortfolioProject..CovidDeaths
Where continent is not Null
Group By date
order by 1,2
-- total_cases, total_death and Death Percentage rate globally
Select sum(new_cases) as total_cases,sum(cast(new_deaths as int)) as total_death,
Round(sum(cast(new_deaths as int))/sum(new_cases)*100,3) as DeathPercentageGlobal
From PortfolioProject..CovidDeaths
Where continent is not Null
-- Joining two tables
Select *
From PortfolioProject..CovidDeaths As Death
Join PortfolioProject..CovidVaccinations As Vacci
On Death.location = Vacci.location
and Death.date = Vacci.date
Order by 3,4
-- Total population vs Vaccination
Select Death.continent,Death.location, Death.date, Death.population, new_vaccinations
, sum(cast(vacci.new_vaccinations as int)) over (Partition By death.location Order By Death.location, Death.Date) As RollingPeopleVaccinated
--, (RollingPeopleVaccinated/population)*100
From PortfolioProject..CovidDeaths As Death
Join PortfolioProject..CovidVaccinations As Vacci
On Death.location = Vacci.location
and Death.date = Vacci.date
Where Death.continent is not Null
Order by 2,3
-- For percentage of population that get vaccinated, use CTE
With PopulationVaccination(Continent, Location, Date, Population, New_vaccinations, RollingPeopleVaccinated)
as
(
Select Death.continent,Death.location, Death.date, Death.population, new_vaccinations
, sum(cast(vacci.new_vaccinations as int)) over (Partition By death.location Order By Death.location, Death.Date) As RollingPeopleVaccinated
--, (RollingPeopleVaccinated/population)*100
From PortfolioProject..CovidDeaths As Death
Join PortfolioProject..CovidVaccinations As Vacci
On Death.location = Vacci.location
and Death.date = Vacci.date
Where Death.continent is not Null
)
Select *, (RollingPeopleVaccinated/Population)*100 as VaccinatedPeople
from PopulationVaccination
-- Using Temp Table to calculate above operation i.e. percentage of vaccinated people
Drop Table if exists #TempTable
Create Table #TempTable(
Continent varchar(50),
Location varchar(50),
Date Datetime,
Population numeric,
New_vaccinaitons numeric,
RollingPeopleVaccinated numeric
)
Insert into #TempTable
Select Death.continent,Death.location, Death.date, Death.population, new_vaccinations
, sum(cast(vacci.new_vaccinations as int)) over (Partition By death.location Order By Death.location, Death.Date) As RollingPeopleVaccinated
--, (RollingPeopleVaccinated/population)*100
From PortfolioProject..CovidDeaths As Death
Join PortfolioProject..CovidVaccinations As Vacci
On Death.location = Vacci.location
and Death.date = Vacci.date
--Where Death.continent is not Null
--Order by 2,3
Select *, (RollingPeopleVaccinated/Population)*100 as VaccinatedPeople
from #TempTable
-- Creating view
Create View PeopleVaccinatedPercentage as
Select Death.continent,Death.location, Death.date, Death.population, new_vaccinations
, sum(cast(vacci.new_vaccinations as int)) over (Partition By death.location Order By Death.location, Death.Date) As RollingPeopleVaccinated
--, (RollingPeopleVaccinated/population)*100
From PortfolioProject..CovidDeaths As Death
Join PortfolioProject..CovidVaccinations As Vacci
On Death.location = Vacci.location
and Death.date = Vacci.date
--Where Death.continent is not Null
--Order by 2,3
Select * from PeopleVaccinatedPercentage