-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQueryCode
157 lines (128 loc) · 5.94 KB
/
QueryCode
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
--SELECT *
--FROM [covid-vaccinations]
SELECT *
FROM [covid-vaccinations]
ORDER BY 3,4
SELECT location, date, total_cases, new_cases, total_deaths, population
FROM COVIDDEATHS
ORDER BY 1,2
--looking at total cases versus total deaths
--SELECT location, date, total_cases, total_deaths, (total_deaths/total_cases) AS DeathPercentage
--FROM COVIDDEATHS
--ORDER BY 1,2
-- query showing the likelihood of dying if you get covid in a specific location, here we opt for converting the datatype into floats for the calculation
SELECT location, date, total_cases, total_deaths, (CONVERT(float,total_deaths)/NULLIF(CONVERT(float,total_cases),0))*100 AS DeathPercentage
FROM COVIDDEATHS
WHERE location like '%states%'
ORDER BY 1,2
-- query showing the likelihood of dying if you get covid in a specific location, no need to convert the numbers as i changed the datatype to numeric in the csv file
SELECT location, date, total_cases, total_deaths, (total_deaths/total_cases)*100 AS DeathPercentage
FROM CovidDeathsNum
WHERE location like '%states%'
ORDER BY 1,2
--Comparing the total cases to the total population
SELECT location, date, total_cases, population, (total_cases/population)*100 AS PopulationCasesPercentage
FROM CovidDeathsNum
WHERE location like '%states%'
ORDER BY 1,2
--Using fetch and offset to see in the middle of the dataset
--SELECT location, date, total_cases, population, (total_cases/population)*100 AS PopulationCasesPercentage
--FROM CovidDeathsNum
--WHERE location like '%states%'
--ORDER BY 1,2 DESC
--OFFSET 200 ROWS
--FETCH NEXT 10 ROWS ONLY
--Looking at countries with highest infection rate compared to population
SELECT location,population, MAX(total_cases)as HighestInfectionCount, Max((total_cases/population))*100 AS PopulationCasesPercentage
FROM CovidDeathsNum
WHERE continent is not null
GROUP BY location, population
ORDER BY PopulationCasesPercentage DESC
--Showing the countries with the highest deathrates, we exclude the regional locations so we can see information for countries
SELECT location, MAX(total_deaths)as TotalDeathCount
FROM CovidDeathsNum
WHERE location NOT IN ('World','High income', 'Upper middle income', 'Europe', 'North America', 'South America', 'Lower middle income', 'European Union','Asia','Africa', 'Low income')
GROUP BY location
ORDER BY TotalDeathCount DESC
--Showing how the same thing would look like if we hadnt converted the total deaths column to numeric before importing
--here we will use typecasting
--Showing the countries with the highest deathrates, we exclude the regional locations so we can see information for countries
--we can also exclude the regions by finding the rows where the continent is null and removing them
SELECT location, MAX(cast(total_deaths as int))as TotalDeathCount
FROM COVIDDEATHS
WHERE continent is not null
GROUP BY location
ORDER BY TotalDeathCount DESC
--breaking things down by continent
SELECT continent, MAX(cast(total_deaths as int))as TotalDeathCount
FROM COVIDDEATHS
WHERE continent is not null
GROUP BY continent
ORDER BY TotalDeathCount DESC
--other way of breaking things down by continent
--SELECT location, MAX(cast(total_deaths as int))as TotalDeathCount
--FROM COVIDDEATHS
--WHERE continent is null
--GROUP BY location
--ORDER BY TotalDeathCount DESC
--Global overview
SELECT date, SUM(new_cases)AS DAILYTOTALNEWCASES, SUM(new_deaths)AS TOTALDAILYNEWDEATHS , (SUM(cast(new_deaths as float))/ NULLIF(SUM(CAST(new_cases as float)),0))*100 AS DeathstoCaseRatio-- total_cases, total_deaths, (total_deaths/total_cases)*100 AS DeathPercentage
FROM CovidDeathsNum
WHERE continent is not null
GROUP BY date
ORDER BY 1,2
SELECT *
FROM CovidVaccinationsNum as vacs
--looking at global vaccinations
SELECT deaths.continent , deaths.location , deaths.date ,vacs.date , deaths.population, vacs.new_vaccinations
FROM CovidDeathsNum as deaths
JOIN CovidVaccinationsNum as vacs
on deaths.location = vacs.location
AND deaths.date = vacs.date
--WHERE vacs.new_vaccinations is not null
--looking at the percentage of the global population that is vaccinated
SELECT deaths.continent , deaths.location , deaths.date, deaths.population, vacs.new_vaccinations, SUM(CONVERT(decimal, vacs.new_vaccinations)) OVER (Partition by deaths.location Order BY deaths.location,deaths.date) AS TotalVaccinationspercountry
FROM CovidDeathsNum as deaths
JOIN CovidVaccinationsNum as vacs
on deaths.location = vacs.location
AND deaths.date = vacs.date
--WHERE vacs.new_vaccinations is not null
WHERE deaths.continent is not null
order by 2,3
-- Using a CTE to get the return of the above query and do another query
WITH PopulationRations (continent, location, date, population, new_Vaccinations, RollingVaccinatedpop)
AS
(
SELECT deaths.continent , deaths.location , deaths.date, deaths.population, vacs.new_vaccinations, SUM(CONVERT(decimal, vacs.new_vaccinations)) OVER (Partition by deaths.location Order BY deaths.location,deaths.date) AS TotalVaccinationspercountry
FROM CovidDeathsNum as deaths
JOIN CovidVaccinationsNum as vacs
on deaths.location = vacs.location
AND deaths.date = vacs.date
--WHERE vacs.new_vaccinations is not null
WHERE deaths.continent is not null
--order by 2,3
)
SELECT*, (RollingVaccinatedpop/ population)*100 AS PercentPopVaccinated
FROM PopulationRations
--temp table
DROP TABLE IF EXISTS #percentpopvaccinated
Create Table #percentpopvaccinated
(
Continent nvarchar(255),
Location nvarchar(255),
Date datetime,
Population numeric,
New_Vaccinations numeric,
RollingpeopleVaccinated numeric
)
INSERT INTO #percentpopvaccinated
(
SELECT deaths.continent , deaths.location , deaths.date, deaths.population, vacs.new_vaccinations, SUM(CONVERT(decimal, vacs.new_vaccinations)) OVER (Partition by deaths.location Order BY deaths.location,deaths.date) AS TotalVaccinationspercountry
FROM CovidDeathsNum as deaths
JOIN CovidVaccinationsNum as vacs
on deaths.location = vacs.location
AND deaths.date = vacs.date
--WHERE vacs.new_vaccinations is not null
WHERE deaths.continent is not null
--order by 2,3
)