-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCOVID Portfolio Project.sql
141 lines (106 loc) · 4.52 KB
/
COVID Portfolio 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
SELECT * FROM
SQLDataExploration..Deaths
WHERE continent is not null
order by 3,4
--SELECT data that we are going to use
SELECT location,date,total_cases,new_cases,total_deaths,population
FROM SQLDataExploration..Deaths order by 1,2
--Looking at total cases vs total deaths
--Shows likelihood of dying if you contract covid in your country
SELECT location,date,total_cases,total_deaths, (total_deaths/total_cases)*100 as death_percentage
FROM SQLDataExploration..Deaths
where location like '%states%' and continent is not null
order by 1,2
--Looking at total cases vs population
--Shows what percentage of population got covid
SELECT location,total_cases,population, (total_cases/population)*100 as covid_percentage
FROM SQLDataExploration..Deaths
--where location like '%states%'
order by 1,2
--Looking at countries with the the highest infection rate
SELECT location,MAX(total_cases) as HighestInfectionCount,population, MAX((total_cases/population))*100 as highestInfectedpercentage
FROM SQLDataExploration..Deaths
--where location like '%states%'
GROUP BY location,population
order by highestInfectedpercentage desc
--Showing countries with the highest death Count per population
SELECT location,MAX(cast(total_deaths as int)) as TotalDeathCount
FROM SQLDataExploration..Deaths
where continent is not null
GROUP BY location
order by TotalDeathCount desc
--LET'S BREAK THINGS DOWN BY continent
--SELECT location,MAX(cast(total_deaths as int)) as TotalDeathCount
--FROM SQLDataExploration..Deaths
--where continent is null
--GROUP BY location
--order by TotalDeathCount desc
--Showing continents with the highest death count
SELECT continent,MAX(cast(total_deaths as int)) as TotalDeathCount
FROM SQLDataExploration..Deaths
where continent is not null
GROUP BY continent
order by TotalDeathCount desc
--GLOBAL numbers
SELECT SUM(new_cases) as total_cases, SUM(cast(new_deaths as int)) as total_deaths, SUM(cast(new_deaths as int))/SUM(new_cases)*100 as death_percentage
FROM SQLDataExploration..Deaths
--where location like '%states%'
where continent is not null
--GROUP BY date
order by 1,2
-- Looking at total population vs vaccinations
SELECT dea.continent,dea.location, dea.date,dea.population, vacc.new_vaccinations
,SUM(CONVERT(int,vacc.new_vaccinations)) OVER (PARTITION BY dea.location order by dea.location, dea.date)
as RollingPeopleVaccinated FROM
SQLDataExploration..Deaths dea JOIN
SQLDataExploration..Vaccinations vacc ON
dea.location = vacc.location and dea.date = vacc.date
where dea.continent is not null
order by 2,3
--Use CTE
With PopvsVacc (continent,location,date,population,new_vaccinations,RollingPeopleVaccinated)
as
(SELECT dea.continent,dea.location, dea.date,dea.population, vacc.new_vaccinations
,SUM(CONVERT(int,vacc.new_vaccinations)) OVER (PARTITION BY dea.location order by dea.location, dea.date)
as RollingPeopleVaccinated FROM
SQLDataExploration..Deaths dea JOIN
SQLDataExploration..Vaccinations vacc ON
dea.location = vacc.location and dea.date = vacc.date
where dea.continent is not null
--order by 2,3
)
SELECT *, (RollingPeopleVaccinated/population)*100 as vaccinatedPercentage
FROM PopvsVacc
--TEMP table
DROP TABLE IF EXISTS #PercentPeopleVaccinated
CREATE TABLE #PercentPeopleVaccinated
(continent nvarchar(255),
location nvarchar(255),
date datetime,
population numeric,
new_vaccinations numeric,
RollingPeopleVaccinated numeric
)
INSERT INTO #PercentPeopleVaccinated
SELECT dea.continent,dea.location, dea.date,dea.population, vacc.new_vaccinations
,SUM(CONVERT(int,vacc.new_vaccinations)) OVER (PARTITION BY dea.location order by dea.location, dea.date)
as RollingPeopleVaccinated FROM
SQLDataExploration..Deaths dea JOIN
SQLDataExploration..Vaccinations vacc ON
dea.location = vacc.location and dea.date = vacc.date
where dea.continent is not null
--order by 2,3
SELECT *, (RollingPeopleVaccinated/population)*100 as vaccinatedPercentage
FROM #PercentPeopleVaccinated
--Creating VIEW to store data for later visualizations
CREATE VIEW PercentPeopleVaccinated
as
SELECT dea.continent,dea.location, dea.date,dea.population, vacc.new_vaccinations
,SUM(CONVERT(int,vacc.new_vaccinations)) OVER (PARTITION BY dea.location order by dea.location, dea.date)
as RollingPeopleVaccinated FROM
SQLDataExploration..Deaths dea JOIN
SQLDataExploration..Vaccinations vacc ON
dea.location = vacc.location and dea.date = vacc.date
where dea.continent is not null
--order by 2,3
SELECT * FROM PercentPeopleVaccinated