-
Notifications
You must be signed in to change notification settings - Fork 0
/
SQL PortfolioProject_1_Data Exploration.sql
174 lines (109 loc) · 4.83 KB
/
SQL PortfolioProject_1_Data Exploration.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 *
from PortfolioProject1..CovidDeaths
order by 3,4
select *
from PortfolioProject1..CovidVaccinations
order by 3,4
Select Location, date, total_cases, new_cases, total_deaths, population
From PortfolioProject1..CovidDeaths
order by 1,2
--total case vs total deaths
Select location, date, total_cases,total_deaths, (CONVERT(float, total_deaths) / NULLIF(CONVERT(float, total_cases), 0)) * 100 AS DeathPercentage
from PortfolioProject1..covidDeaths
where location like '%canada%'
order by 1,2
--total case vs total deaths -- my way
Select location, date, total_cases,total_deaths, (cast(total_deaths as float) / cast (total_cases as float)) * 100 AS DeathPercentage
from PortfolioProject1..covidDeaths
where location like '%canada%'
order by 1,2
--total case vs total population
Select location, date, total_cases,population, (total_cases / population) * 100 AS AffectedPercentage
from PortfolioProject1..covidDeaths
where location like '%canada%'
order by 1,2
--Countries with highest infection rate
Select location, population, max(total_cases) as HighestInfectionCount, max((total_cases / population)) * 100 AS AffectedPercentage
from PortfolioProject1..CovidDeaths
--where location like '%canada%'
Group by location, population
order by AffectedPercentage desc
-- Countries with highest death
Select location, max(cast(total_deaths as int)) as TotalDeath
from PortfolioProject1..CovidDeaths
--where location like '%canada%'
Group by location
order by TotalDeath desc
--global number datewise
Select Date, sum(new_cases) as total_cases, SUM(cast(new_deaths as int)) as total_deaths, sum(cast(new_deaths as int))/sum(Nullif(New_Cases,0))* 100 AS DeathPercentage
from PortfolioProject1..covidDeaths
--where location like '%canada%'
where continent is not null
group by Date
order by 1,2
--global number total
Select sum(new_cases) as total_cases, SUM(cast(new_deaths as int)) as total_deaths, sum(cast(new_deaths as int))/sum(Nullif(New_Cases,0))* 100 AS DeathPercentage
from PortfolioProject1..covidDeaths
--where location like '%canada%'
where continent is not null
--group by Date
order by 1,2
-- total population vs vaccinations
select dea.continent, dea.location, dea.date, dea.population, vac.new_vaccinations
, SUM (cast(vac.new_vaccinations as bigint)) over (partition by dea.location order by dea.location , dea.date) as RollingPeopleVaccinated
from PortfolioProject1..CovidDeaths dea
Join PortfolioProject1..CovidVaccinations vac
on dea.location = vac.location
and dea.date = vac.date
where dea.continent is not null and vac.new_vaccinations is not null
order by 2,3
-- total population vs vaccinations
--using CTE
With PopvsVac(Continent, Location, date, population, new_vaccinations, RollingPeopleVaccinated)
as
(
select dea.continent, dea.location, dea.date, dea.population, vac.new_vaccinations
, SUM (cast(vac.new_vaccinations as bigint)) over (partition by dea.location order by dea.location , dea.date) as RollingPeopleVaccinated
from PortfolioProject1..CovidDeaths dea
Join PortfolioProject1..CovidVaccinations vac
on dea.location = vac.location
and dea.date = vac.date
where dea.continent is not null and vac.new_vaccinations is not null
--order by 2,3
)
select *, (RollingPeopleVaccinated/population)*100 as RollingPeopleVaccinatedPercentage
from PopvsVac
--Temp Table
Drop table if exists #PercentPopulationVaccinated
create table #PercentPopulationVaccinated
(
Continent nvarchar(255),
Location nvarchar(255),
Date datetime,
Population numeric,
New_Vaccinations numeric,
RollingPeopleVaccinated numeric,
)
insert into #PercentPopulationVaccinated
select dea.continent, dea.location, dea.date, dea.population, vac.new_vaccinations
, SUM (cast(vac.new_vaccinations as bigint)) over (partition by dea.location order by dea.location , dea.date) as RollingPeopleVaccinated
from PortfolioProject1..CovidDeaths dea
Join PortfolioProject1..CovidVaccinations vac
on dea.location = vac.location
and dea.date = vac.date
--where dea.continent is not null and vac.new_vaccinations is not null
--order by 2,3
select *, (RollingPeopleVaccinated/population)*100
from #PercentPopulationVaccinated
--Creating view to store data for visualization
create view PercentPopulationVaccinated as
select dea.continent, dea.location, dea.date, dea.population, vac.new_vaccinations
, SUM (cast(vac.new_vaccinations as bigint)) over (partition by dea.location order by dea.location , dea.date) as RollingPeopleVaccinated
from PortfolioProject1..CovidDeaths dea
Join PortfolioProject1..CovidVaccinations vac
on dea.location = vac.location
and dea.date = vac.date
where dea.continent is not null and vac.new_vaccinations is not null
--order by 2,3
select *
from PercentPopulationVaccinated