-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsql_data_exploration_script.sql
More file actions
238 lines (231 loc) · 6.68 KB
/
sql_data_exploration_script.sql
File metadata and controls
238 lines (231 loc) · 6.68 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
/*
Author: Augusto Rosa
Source: Our World in Data Covid 19
Description: Perform a data exploration analysis using MS SQL Server
Language Used: SQL, T-SQL
Skills used: Joins, CTE's, Temp Tables, Windows Functions, Aggregate Functions, Creating Views, Converting Data Types
*/
--
-- Ordering both tables for further analysis
--
SELECT *
FROM staging_data.dbo.tb_covid_deaths
ORDER BY 3,4
--
SELECT *
FROM staging_data.dbo.tb_covid_vaccinations
ORDER BY 3,4
--
SELECT
location
,date
,total_cases
,new_cases
,total_deaths
,population
FROM staging_data.dbo.tb_covid_deaths
ORDER BY 1,2
--
-- Selecting only countries (not null continent)
--
SELECT *
FROM staging_data.dbo.tb_covid_deaths
WHERE 1=1
AND continent IS NOT NULL
ORDER BY 3,4
--
-- Select initial data to begin the analysis
--
SELECT
Location
,date
,total_cases
,new_cases
,total_deaths
,population
FROM staging_data.dbo.tb_covid_deaths
WHERE 1=1
AND continent IS NOT NULL
ORDER BY 1,2
--
-- Total Cases vs Total Deaths
-- Shows the probability of dying if you contract COVID-19 in your country
--
SELECT
Location
,date
,total_cases
,total_deaths
,(CAST(total_deaths AS DECIMAL(10,2))/total_cases) * 100 AS deathPercentage -- Converted to decimal to avoid integer truncation (int/int returns 0)
From staging_data.dbo.tb_covid_deaths
WHERE 1=1
AND location = 'Canada'
AND continent IS NOT NULL
ORDER BY 1,2
--
-- Total Cases vs Population
-- Shows the percentage of the population infected with COVID-19
--
SELECT
Location
,date
,Population
,total_cases
,(CAST(total_cases AS DECIMAL(10,2))/population) * 100 AS PercentPopulationInfected -- -- Converted to decimal to avoid integer truncation
FROM staging_data.dbo.tb_covid_deaths
--WHERE 1=1
--AND location = 'Canada'
ORDER BY 1,2
--
-- Countries with Highest Infection rate compared to Population
--
SELECT
Location
,Population
,MAX(total_cases) AS HighestInfectionCount
,Max((total_cases/population)) * 100 AS PercentPopulationInfected
FROM staging_data.dbo.tb_covid_deaths
--WHERE 1=1
--AND location = 'Canada'
GROUP BY Location, Population
ORDER BY PercentPopulationInfected DESC
--
-- Countries with Highest Death Count per Population
--
SELECT
Location
,MAX(CAST(Total_deaths AS INT)) AS TotalDeathCount
FROM staging_data.dbo.tb_covid_deaths
WHERE 1=1
--AND location = 'Canada'
AND continent IS NOT NULL
AND location NOT IN ('World', 'Europe', 'North America', 'European Union', 'South America', 'Africa', 'Oceania', 'Asia') -- Apenas paises e não continentes...
GROUP BY Location
ORDER BY TotalDeathCount DESC
--
-- BREAKING INFORMATION DOWN BY CONTINENT
--
-- Showing contintents with the highest death count per population
SELECT
continent
,MAX(CAST(Total_deaths AS INT)) AS TotalDeathCount
FROM staging_data.dbo.tb_covid_deaths
WHERE 1=1
--AND location = 'Canada'
AND continent IS NOT NULL
AND continent <> ''
GROUP BY continent
ORDER BY TotalDeathCount DESC
--
-- GLOBAL NUMBERS
--
SELECT
SUM(CAST(new_cases AS INT)) AS TotalCases
,SUM(CAST(new_deaths AS INT)) AS TotalDeaths
,CAST(ROUND(CAST(SUM(CAST(new_deaths AS INT)) AS FLOAT)/CAST(SUM(CAST(New_Cases AS INT)) AS FLOAT) * 100, 2) AS VARCHAR(5)) + '%' AS DeathPercentage -- Converted from VARCHAR to INT, then to FLOAT to ensure accurate division
FROM staging_data.dbo.tb_covid_deaths
WHERE 1=1
--AND location = 'Canada'
AND continent IS NOT NULL
--GROUP BY date
ORDER BY 1,2
--
-- Total Population vs Vaccinations
-- Shows Percentage of Population that has recieved at least one Covid Vaccine
--
SELECT
dea.continent
,dea.location
,dea.date
,dea.population
,vac.new_vaccinations
,SUM(CONVERT(INT,vac.new_vaccinations)) OVER (PARTITION BY dea.Location ORDER BY dea.location, dea.Date) AS TotalPeopleVaccinated
--,(RollingPeopleVaccinated/population)*100 -> Calculated Later on
FROM staging_data.dbo.tb_covid_deaths dea
INNER JOIN staging_data.dbo.tb_covid_vaccinations vac ON (dea.location = vac.location) AND (dea.date = vac.date)
WHERE 1=1
AND dea.continent IS NOT NULL
ORDER BY 2,3
--
-- Using CTE to calculate cumulative vaccinations by country
--
With Peoplevsvaccinations (Continent, Location, Date, Population, New_Vaccinations, TotalPeopleVaccinated)
AS
(
SELECT
dea.continent
,dea.location
,dea.date
,NULLIF(dea.population, 0) AS population
,vac.new_vaccinations
,SUM(CONVERT(INT,vac.new_vaccinations)) OVER (PARTITION BY dea.Location ORDER BY dea.location, dea.Date) AS TotalPeopleVaccinated
FROM staging_data.dbo.tb_covid_deaths dea
INNER JOIN staging_data.dbo.tb_covid_vaccinations vac ON (dea.location = vac.location) AND (dea.date = vac.date)
WHERE 1=1
AND dea.continent IS NOT NULL
AND dea.continent <> ''
)
SELECT *
,CAST(ROUND((TotalPeopleVaccinated/CAST(Population AS FLOAT)) * 100, 2) AS VARCHAR(10)) + '%' AS PercentagePopulationVaccinated
FROM Peoplevsvaccinations
ORDER BY 1 ASC
--
-- Using Temp Table to calculate the same result (percentage of population vaccinated)
--
DROP TABLE IF EXISTS #PercentPopulationVaccinated
CREATE TABLE #PercentPopulationVaccinated
(
Continent nvarchar(50),
Location nvarchar(80),
Date datetime,
Population numeric,
new_vaccinations numeric,
TotalPeopleVaccinated numeric
)
--
-- Inserting data into Temp Table
--
INSERT INTO #PercentPopulationVaccinated
SELECT
dea.continent
,dea.location
,dea.date
,NULLIF(TRY_CAST(dea.population AS NUMERIC), 0) AS population
,TRY_CAST(vac.new_vaccinations AS NUMERIC) AS new_vaccinations
,SUM(CONVERT(INT,vac.new_vaccinations)) OVER (PARTITION BY dea.Location ORDER BY dea.location, dea.Date) AS TotalPeopleVaccinated
FROM staging_data.dbo.tb_covid_deaths dea
INNER JOIN staging_data.dbo.tb_covid_vaccinations vac ON (dea.location = vac.location) AND (dea.date = vac.date)
WHERE 1=1
AND dea.continent IS NOT NULL
AND dea.continent <> ''
--
-- Selecting final result from Temp Table #PercentPopulationVaccinated
--
SELECT *
,CAST(ROUND((TotalPeopleVaccinated/CAST(Population AS FLOAT)) * 100, 2) AS VARCHAR(10)) + '%' AS PercentagePopulationVaccinated
FROM #PercentPopulationVaccinated
ORDER BY 1 ASC
--
-- Creating a view to store the data for future visualizations
--
DROP VIEW IF EXISTS vw_PercentPopulationVaccinated -- Dropping the view if it already exists
--
CREATE VIEW vw_PercentPopulationVaccinated AS
SELECT
dea.continent
,dea.location
,dea.date
,NULLIF(TRY_CAST(dea.population AS NUMERIC), 0) AS population
,TRY_CAST(vac.new_vaccinations AS NUMERIC) AS new_vaccinations
,SUM(CONVERT(INT,vac.new_vaccinations)) OVER (PARTITION BY dea.Location ORDER BY dea.location, dea.Date) AS TotalPeopleVaccinated
FROM staging_data.dbo.tb_covid_deaths dea
INNER JOIN staging_data.dbo.tb_covid_vaccinations vac ON (dea.location = vac.location) AND (dea.date = vac.date)
WHERE 1=1
AND dea.continent IS NOT NULL
AND dea.continent <> '';
--
-- Viewing the data stored in the created view
--
SELECT *
FROM vw_PercentPopulationVaccinated
ORDER BY 1 ASC