-
Notifications
You must be signed in to change notification settings - Fork 0
/
Portfolio_Covid19_Stats.sql
147 lines (110 loc) · 4.5 KB
/
Portfolio_Covid19_Stats.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
/*
COVID-19 Data Exploration
Data Date Range '01/01/2020-04/25/2022'
Global Cases, Global Deaths, Death Rate percentage, Infection Rates, Joins, & Vaccinations
*/
Select *
From PortfolioProjects..CovidDeaths_v1$
Order By 3,4
Select *
From PortfolioProjects..CovidVaccinations_v1$
Order By 3,4
--Select Data used
Select Location, date, total_cases, new_cases, total_deaths, (total_deaths/total_cases)*100 as Death_Percentage
From PortfolioProjects..CovidDeaths_v1$
Where location like '%states%'
Order By 1,2
--Looking at Total Cases vs Total Deaths
-- Percentage rate of Death in US
Select Location, date, total_cases, new_cases, total_deaths, (total_deaths/total_cases)*100 as Death_Percentage
From PortfolioProjects..CovidDeaths_v1$
Where location like '%states%'
Order By 1,2
-- Total Cases vs Population
-- Population % w/ C19
Select Location, date, total_cases, Population, (total_cases/population)*100 as CovidCases_Percentage
From PortfolioProjects..CovidDeaths_v1$
Where location like '%states%'
Order By 1,2
--Infection Rates Amoung Countries
Select Location, population, MAX(total_cases) as Infection_Rate, MAX((total_cases/population))*100 as InfectionRate_Percentage
From PortfolioProjects..CovidDeaths_v1$
Group By location, population
Order By InfectionRate_Percentage desc
-- By Continent
Select continent, Max(cast(total_deaths as bigint)) as TotalDeathCount
From PortfolioProjects..CovidDeaths_v1$
Where continent is not null
Group By continent
Order By TotalDeathCount desc
-- Continents w/ Highest Death Count
Select location, Max(cast(total_deaths as bigint)) as TotalDeathCount
From PortfolioProjects..CovidDeaths_v1$
Where continent is null
Group By location
Order By TotalDeathCount desc
--Country Death Count
Select Location, Max(cast(total_deaths as bigint)) as TotalDeathCount
From PortfolioProjects..CovidDeaths_v1$
Where continent is not null
Group By location
Order By TotalDeathCount desc
-- Global Numbers
Select SUM(new_cases) as Total_Cases, Sum(cast(new_deaths as bigint)) as Total_Deaths, Sum(cast(new_deaths as bigint))/Sum(new_cases)*100 as Death_Percentage
From PortfolioProjects..CovidDeaths_v1$
Where continent is not null
-- Group by Date
Order by 1,2
-- Covid Vaccinations
Select *
From PortfolioProjects..CovidVaccinations_v1$
--Joining Covid Deaths and Covid Vacs by date and location
Select *
From PortfolioProjects..CovidDeaths_v1$ dea
Join PortfolioProjects..CovidVaccinations_v1$ vac
On dea.location = vac.location
and dea.date = vac.date
--Total Population vs Vaccinations
With populationVSvaccination (Continent, Location, Date, Population, New_Vaccinations, Vaccination_Rollover)
as
(
Select dea.continent, dea.location, dea.date, dea.population, vac.new_vaccinations, SUM(convert( bigint, vac.new_vaccinations)) OVER (Partition by dea.location order by dea.location, dea.date) as Vaccination_Rollover
From PortfolioProjects..CovidDeaths_v1$ dea
Join PortfolioProjects..CovidVaccinations_v1$ vac
On dea.location = vac.location
and dea.date = vac.date
where dea.continent is not null
--order by 2,3
)
Select*, (Vaccination_Rollover/Population)*100
From populationVSvaccination
-- Temp Table
Drop Table if exists #PercentPopulationVaccinated
Create Table #PercentPopulationVaccinated
(
Continent nvarchar(255),
Location nvarchar(255),
Date datetime,
Population numeric,
New_Vacinations numeric,
Vaccination_Rollover numeric
)
Insert into #PercentPopulationVaccinated
Select dea.continent, dea.location, dea.date, dea.population, vac.new_vaccinations, SUM(convert(bigint, vac.new_vaccinations)) OVER (Partition by dea.location order by dea.location, dea.date) as Vaccination_Rollover
From PortfolioProjects..CovidDeaths_v1$ dea
Join PortfolioProjects..CovidVaccinations_v1$ vac
On dea.location = vac.location
and dea.date = vac.date
--where dea.continent is not null
--order by 2,3
Select*, (Vaccination_Rollover/Population)*100 as Percent_Vaccinated
From #PercentPopulationVaccinated
-- View Creation for Visualizations
Create View PercentPopulationVaccinated_v2
as
Select dea.continent, dea.location, dea.date, dea.population, vac.new_vaccinations, SUM(convert(bigint, vac.new_vaccinations)) OVER (Partition by dea.location order by dea.location, dea.date) as Vaccination_Rollover
From PortfolioProjects..CovidDeaths_v1$ dea
Join PortfolioProjects..CovidVaccinations_v1$ vac
On dea.location = vac.location
and dea.date = vac.date
where dea.continent is not null