-
Notifications
You must be signed in to change notification settings - Fork 0
/
SQLQuery1.sql
173 lines (144 loc) · 5.52 KB
/
SQLQuery1.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
Select *
From [Covid_19 ]..covid_deaths
Where continent is not null
order by 3,4
--Select *
--From [Covid_19 ]..covid_vaccinations
--order by 3,4
-- Select data that we are going to be using
Select location, date, total_cases, new_cases, total_deaths, population
From [Covid_19 ]..covid_deaths
Where continent is not null
order by 1,2
-- Looking at Total Cases vs Total Deaths
Select location, date, total_cases, total_deaths,
CASE
WHEN TRY_CAST(total_cases AS float)*100 = 0 THEN NULL
ELSE total_deaths / TRY_CAST(total_cases AS float)*100
END AS death_Precentage
From [Covid_19 ]..covid_deaths
Where location like '%latvia%'
and continent is not null
order by 1,2
-- total cases vs population
-- shows what percentage of population got Covid
-- shows what bagg
Select location, population, total_cases, date
From [Covid_19 ]..covid_deaths
where location like '%cyprus%'
and continent is not null
--GROUP BY location, population
ORDER BY date desc;
Select location, population, MAX((total_cases)) as HighestInfectionCount
From [Covid_19 ]..covid_deaths
where location like '%cyprus%'
and continent is not null
GROUP BY location, population
ORDER BY HighestInfectionCount desc;
--Countries with highest infection rate compared to Population
Select location, population, MAX(cast(total_cases as int)) as HighestInfectionCount, Max((total_cases/population))*100 as PercentPopulationInfected
From [Covid_19 ]..covid_deaths
--where location like '%latvia%'
Where continent is not null
Group by location, population
order by PercentPopulationInfected desc;
-- Breaking things down by continent
Select location, MAX(cast(total_deaths as int)) as TotalDeaths, MAX((total_deaths/population))*100 as PearcentageOfDeaths
From [Covid_19 ]..covid_deaths
--where location like '%latvia%'
Where continent is null
Group by location
order by PearcentageOfDeaths desc;
-- Showing countries with highest Death Count per Population
Select location, MAX(cast(total_deaths as int)) as TotalDeaths, MAX((total_deaths/population))*100 as PearcentageOfDeaths
From [Covid_19 ]..covid_deaths
where location like '%states%'
and continent is not null
Group by location
order by PearcentageOfDeaths desc;
-- Showing continents with the highest death count per population
Select location, MAX(cast(total_deaths as int)) as TotalDeaths, MAX((total_deaths/population))*100 as PearcentageOfDeaths
From [Covid_19 ]..covid_deaths
--where location like '%latvia%'
Where continent is null
Group by location
order by PearcentageOfDeaths desc;
-- Gloabal numbers
Select SUM(cast(new_cases as int)) as Total_cases, SUM(cast(new_deaths as int)) as Total_deaths, COALESCE(SUM(CAST(new_deaths AS int)) / NULLIF(SUM(new_cases), 0) * 100, 0) AS death_percentage
From [Covid_19 ]..covid_deaths
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, vac.new_vaccinations
, SUM(convert(bigint, vac.new_vaccinations)) over (Partition by dea.location Order by dea.location,
dea.date) as RollingPeopleVaccinated
From [Covid_19 ]..covid_deaths dea
Join [Covid_19 ]..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(convert(bigint, vac.new_vaccinations)) over (Partition by dea.location)
From [Covid_19 ]..covid_deaths dea
Join [Covid_19 ]..covid_vaccinations vac
on dea.location = vac.location
and dea.date = vac.date
Where dea.continent is not null
--order by 2,3
)
Select *, (RollingPeopleVaccinated/population)*100
From PopvsVac
-- 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(convert(bigint, vac.new_vaccinations)) over (Partition by dea.location)
From [Covid_19 ]..covid_deaths dea
Join [Covid_19 ]..covid_vaccinations vac
on dea.location = vac.location
and dea.date = vac.date
Where dea.continent is not null
--order by 2,3
Select *, (RollingPeopleVaccinated/population)*100
From #PercentPopulationVaccinated
-- Creating view to store data for later visualizations
USE [Covid_19 ]
go
Create View PercentVaccinatedPopulation 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 RollingPeopleVaccinated
From
[Covid_19 ]..covid_deaths dea
Join
[Covid_19 ]..covid_vaccinations vac
on
dea.location = vac.location
and dea.date = vac.date
Where
dea.continent is not null
--order by 2,3
Select location, MAX(cast(total_deaths as int)) as TotalDeaths, MAX((total_deaths/population))*100 as PearcentageOfDeaths
From [Covid_19 ]..covid_deaths
--where location like '%latvia%'
Where continent is null
Group by location
order by PearcentageOfDeaths desc;
Select *
From PercentVaccinatedPopulation