-
Notifications
You must be signed in to change notification settings - Fork 0
/
COVIDSQLQUERY.sql
176 lines (137 loc) · 5.17 KB
/
COVIDSQLQUERY.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
175
176
SELECT *
FROM pproject..['COVID Deaths$']
ORDER BY 3,4
SELECT *
FROM pproject..['COVID Vaccinations$']
ORDER BY 3,4
SELECT Location,
Date,
total_cases,
new_cases,
total_deaths,
population
FROM pproject..['COVID 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,
(CONVERT(float, total_deaths) / NULLIF(CONVERT(float, total_cases), 0)) * 100 AS Deathpercentage
FROM pproject..['COVID Deaths$']
WHERE location like 'United States'
AND total_cases IS NOT NULL
AND continent is not null
ORDER BY 1,2
-- Looking at Total Cases vs Population
-- Shows what percentage of population got COVID
SELECT Location,
Date,
population,
total_cases,
(CONVERT(float, total_cases) / NULLIF(CONVERT(float, population), 0)) * 100 AS CovidAffectedPercentage
FROM pproject..['COVID Deaths$']
WHERE location like 'United States'
AND total_cases IS NOT NULL
AND continent IS NOT NULL
ORDER BY 1,2
-- Looking at Countries with Highest Infection Rate compared to Population
SELECT Location,
population,
MAX(total_cases) as HighestInfectionCount,
(CONVERT(float, MAX(total_cases)) / NULLIF(CONVERT(float, population), 0)) * 100 AS CovidAffectedPercentage
FROM pproject..['COVID Deaths$']
GROUP BY location, population
ORDER BY CovidAffectedPercentage DESC
--LET'S BREAK THINGS DOWN BY CONTINENT
SELECT continent,
MAX(cast(total_deaths as int)) as TotalDeathCount
FROM pproject..['COVID Deaths$']
WHERE continent is not null
GROUP BY continent
ORDER BY TotalDeathCount DESC
--Showing Countries with Highest Death Count
SELECT Location,
MAX(cast(total_deaths as int)) as TotalDeathCount
FROM pproject..['COVID Deaths$']
WHERE continent is not null
GROUP BY location
ORDER BY TotalDeathCount DESC
--Showing Countries with Highest Death Count to Population
SELECT Location,
MAX(CAST(total_deaths AS INT)) / NULLIF(CONVERT(FLOAT, MAX(population)), 0)*100 AS DeathCounttoPopRatio
FROM pproject..['COVID Deaths$']
WHERE continent IS NOT NULL
GROUP BY Location
ORDER BY DeathCounttoPopRatio DESC;
-- GLOBAL NUMBERS
SELECT Date,
SUM(new_cases) AS TotalCases,
SUM(cast(new_deaths as int)) as TotalDeaths,
SUM(cast(new_deaths as int))/SUM(New_cases)*100 as DeathPercentage
FROM pproject..['COVID Deaths$']
WHERE total_Cases is not null AND continent is not null
GROUP BY date
ORDER BY 1,2
SELECT SUM(new_cases) AS TotalCases,
SUM(cast(new_deaths as int)) as TotalDeaths,
SUM(cast(new_deaths as int))/SUM(New_cases)*100 as DeathPercentage
FROM pproject..['COVID Deaths$']
WHERE total_Cases is not null AND continent is not null
ORDER BY 1,2
-- Looking at 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 pproject..['COVID Deaths$'] dea
JOIN pproject..['COVID Vaccinations$'] vac
ON dea.location = vac.location
AND dea.date = vac.date
WHERE dea.continent is not null
ORDER BY 2,3
--USE 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.date) AS RollingPeopleVaccinated
FROM pproject..['COVID Deaths$'] dea
JOIN pproject..['COVID Vaccinations$'] vac ON dea.location = vac.location AND dea.date = vac.date
WHERE dea.continent IS NOT NULL
)
SELECT *, (RollingPeopleVaccinated/Population) as PercentageofPopVaccinated
FROM PopvsVac
ORDER BY Location, Date;
--USE 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.date) AS RollingPeopleVaccinated
FROM pproject..['COVID Deaths$'] dea
JOIN pproject..['COVID Vaccinations$'] vac ON dea.location = vac.location AND dea.date = vac.date
WHERE dea.continent IS NOT NULL
SELECT *, (RollingPeopleVaccinated/Population) as PercentageofPopVaccinated
FROM #PercentPopulationVaccinated
--Creating View to store data for later visualizations
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.date) AS RollingPeopleVaccinated
FROM pproject..['COVID Deaths$'] dea
JOIN pproject..['COVID Vaccinations$'] vac ON dea.location = vac.location AND dea.date = vac.date
WHERE dea.continent IS NOT NULL