-
Notifications
You must be signed in to change notification settings - Fork 0
/
sql_exploration_covid_vaccines.sql
104 lines (87 loc) · 4.56 KB
/
sql_exploration_covid_vaccines.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
SELECT iso_code, continent, location
FROM `covid-19-pandemic-385422.covid_vaccines.covid_vaccines`
WHERE location = "United States"
LIMIT 1000
SELECT *
FROM `covid-19-pandemic-385422.covid_vaccines.covid_vaccines`
LIMIT 1000
-- Joined tables covid_death and covid_vaccine
SELECT *
FROM `covid-19-pandemic-385422.covid_deaths.covid_death` AS dea
JOIN `covid-19-pandemic-385422.covid_vaccines.covid_vaccines` AS vac
ON dea.location = vac.location
AND dea.date = vac.date
-- Looking at Total Population vs new Vaccinations on a daily basis.
SELECT dea.continent, dea.location, dea.date, dea.population, vac.new_vaccinations
FROM `covid-19-pandemic-385422.covid_deaths.covid_death` AS dea
JOIN `covid-19-pandemic-385422.covid_vaccines.covid_vaccines` AS vac
ON dea.location = vac.location
AND dea.date = vac.date
WHERE dea.continent IS NOT NULL
ORDER BY 2, 3
-- Looking at Total Population vs new Vaccinations on a daily basis and running total
SELECT dea.continent, dea.location, dea.date, dea.population, vac.new_vaccinations, SUM(CAST(vac.new_vaccinations as int)) OVER (PARTITION BY dea.location ORDER BY dea.location, dea.date) AS runningct_ppl_vac
FROM `covid-19-pandemic-385422.covid_deaths.covid_death` AS dea
JOIN `covid-19-pandemic-385422.covid_vaccines.covid_vaccines` AS vac
ON dea.location = vac.location
AND dea.date = vac.date
WHERE dea.continent IS NOT NULL
ORDER BY 2, 3
-- Looking at Total Population vs new Vaccinations on a daily basis and running total
-- Using CTE but not able to run alias
WITH dea as (SELECT continent, location, date, population FROM `covid-19-pandemic-385422.covid_deaths.covid_death` ),
vac as (SELECT new_vaccinations, location, date FROM `covid-19-pandemic-385422.covid_vaccines.covid_vaccines`)
SELECT dea.continent, dea.location, dea.date, dea.population, vac.new_vaccinations, SUM(CAST(vac.new_vaccinations as int)) OVER (PARTITION BY dea.location ORDER BY dea.location, dea.date) AS runningct_ppl_vac
-- , (runningct_ppl_vac/population)*100 AS percent_vaccinated
FROM dea
JOIN vac
ON (dea.location = vac.location
AND dea.date = vac.date)
WHERE dea.continent IS NOT NULL
-- ORDER BY 2, 3
-- Looking at Total Population vs new Vaccinations on a daily basis and running total and percentage of vaccinated on each day
-- Using Common Table Expression (CTE) This allows the use of alias in SELECT clause because of use of WITH
WITH popvsvac as (
SELECT dea.continent, dea.location, dea.date, dea.population, vac.new_vaccinations, SUM(CAST(vac.new_vaccinations as int)) OVER (PARTITION BY dea.location ORDER BY dea.location, dea.date) AS runningct_ppl_vac
FROM `covid-19-pandemic-385422.covid_deaths.covid_death` AS dea
JOIN `covid-19-pandemic-385422.covid_vaccines.covid_vaccines` AS vac
ON dea.location = vac.location
AND dea.date = vac.date
WHERE dea.continent IS NOT NULL
-- ORDER BY 2, 3
)
SELECT *, (runningct_ppl_vac/population)*100 AS percent_vaccinated
FROM popvsvac
-- TEMP TABLE
DROP TABLE IF exists `covid-19-pandemic-385422.covid_deaths.percent_pop_vac`;
CREATE TABLE `covid-19-pandemic-385422.covid_deaths.percent_pop_vaccinated` (
continent string,
location string,
date date,
population integer,
new_vaccinations integer,
runningct_ppl_vac numeric
);
INSERT INTO `covid-19-pandemic-385422.covid_deaths.percent_pop_vaccinated`
(
SELECT dea.continent, dea.location, dea.date, dea.population, vac.new_vaccinations, SUM(CAST(vac.new_vaccinations as int)) OVER (PARTITION BY dea.location ORDER BY dea.location, dea.date) AS runningct_ppl_vac
FROM `covid-19-pandemic-385422.covid_deaths.covid_death` AS dea
JOIN `covid-19-pandemic-385422.covid_vaccines.covid_vaccines` AS vac
ON dea.location = vac.location
AND dea.date = vac.date
WHERE dea.continent IS NOT NULL
-- ORDER BY 2, 3
);
SELECT *, (runningct_ppl_vac/population)*100 AS percent_pop_vac
FROM `covid-19-pandemic-385422.covid_deaths.percent_pop_vaccinated`
-- Creating View to store data for later visualizations and I should create Views based on all the different sql functions written here.
CREATE VIEW `covid-19-pandemic-385422.covid_deaths.percent_population_vaccinated` AS
SELECT dea.continent, dea.location, dea.date, dea.population, vac.new_vaccinations, SUM(CAST(vac.new_vaccinations as int)) OVER (PARTITION BY dea.location ORDER BY dea.location, dea.date) AS runningct_ppl_vac
FROM `covid-19-pandemic-385422.covid_deaths.covid_death` AS dea
JOIN `covid-19-pandemic-385422.covid_vaccines.covid_vaccines` AS vac
ON dea.location = vac.location
AND dea.date = vac.date
WHERE dea.continent IS NOT NULL
-- ORDER BY 2, 3
SELECT *
FROM `covid-19-pandemic-385422.covid_deaths.percent_population_vaccinated`