-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCovidProject_Script.sql
114 lines (101 loc) · 4.42 KB
/
CovidProject_Script.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
Select *
From CovidDeaths
order by 3,4
Select *
From PortfolioProject..CovidVaccinations
order by 3,4
Select location, date, total_cases, new_cases, total_deaths, population
From PortfolioProject..CovidDeaths
order by 1,2
--Looking at Total Cases vs Total Deaths
--shows the likelihood of dying if you contract covid in your country
Select location, date, total_cases, total_deaths, cast((total_deaths) as float)/total_cases *100 as DeathPercentage
From PortfolioProject..CovidDeaths
Where location LIKE '%states%'
order by 1,2
--Looking at the total cases vs population
--shows what percentage of popuation got Covid in Nepal
Select location, date, total_cases, population, cast((total_cases) as float)/population *100 as PercentageInfected
From PortfolioProject..CovidDeaths
Where location= 'Nepal'
order by 1,2
--Looking at countries with highest infection rate compared to population
Select Location, Population, MAX(total_cases) as HighestInfectionCount, MAX(cast((total_cases) as float)/population *100) as PercentInfected
From PortfolioProject..CovidDeaths
Group by Location, Population
Order by PercentInfected DESC
--Showing countries with highest death
Select Location, MAX(total_deaths) as TotalDeathCount
From PortfolioProject..CovidDeaths
where continent is not null --row with continent null has continent as the country
Group by Location
Order by TotalDeathCount desc
--Breaking it out by continent
Select Continent, MAX(total_deaths) as TotalDeathCount
From PortfolioProject..CovidDeaths
where continent is not null --row with continent null has continent as the country
Group by Continent
Order by TotalDeathCount desc
--Global Numbers by Date
Select date, SUM(total_cases) as Total_Cases, SUM(cast(total_deaths as int)) as Total_Deaths, SUM(cast(new_deaths as float))/SUM(New_Cases)*100 as DeathPercentage
From PortfolioProject..CovidDeaths
where continent is not NULL
Group By date
Order by 1
--Looking at total population vs vaccination
Select dea.continent, dea.location, dea.date, dea.population, vac.new_vaccinations
From PortfolioProject..CovidDeaths dea
Join PortfolioProject..CovidVaccinations vac
On dea.location=vac.LOCATION
and dea.date=vac.date
where dea.continent is not NULL
order by 2,3
--Sum of vaccinations
Select dea.continent, dea.location, dea.date, dea.population, vac.new_vaccinations, SUM(vac.new_vaccinations) OVER (Partition by dea.location Order by dea.location, dea.date) as RollingPeopleVaccinated
From PortfolioProject..CovidDeaths dea
Join PortfolioProject..CovidVaccinations vac
On dea.location=vac.LOCATION
and dea.date=vac.date
where dea.continent is not NULL
order by 2,3
--Using Common Table Expression (CTE)
With PopvsVac (Continent, Location, Date, Population, New_Vaccinations, RollingPeopleVaccinated)
AS
(
Select dea.continent, dea.location, dea.date, dea.population, vac.new_vaccinations, SUM(vac.new_vaccinations) OVER (Partition by dea.location Order by dea.location, dea.date) as RollingPeopleVaccinated
From PortfolioProject..CovidDeaths dea
Join PortfolioProject..CovidVaccinations vac
On dea.location=vac.LOCATION
and dea.date=vac.date
where dea.continent is not NULL
)
Select *, (RollingPeopleVaccinated/cast(Population as float))*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(vac.new_vaccinations) OVER (Partition by dea.location Order by dea.location, dea.date) as RollingPeopleVaccinated
From PortfolioProject..CovidDeaths dea
Join PortfolioProject..CovidVaccinations vac
On dea.location=vac.LOCATION
and dea.date=vac.date
where dea.continent is not NULL
Select *, (RollingPeopleVaccinated/cast(Population as float))*100
From #PercentPopulationVaccinated
--Creating View to store data for later visualizations
Create View PercentPopulationVaccinated AS
Select dea.continent, dea.location, dea.date, dea.population, vac.new_vaccinations, SUM(vac.new_vaccinations) OVER (Partition by dea.location Order by dea.location, dea.date) as RollingPeopleVaccinated
From PortfolioProject..CovidDeaths dea
Join PortfolioProject..CovidVaccinations vac
On dea.location=vac.LOCATION
and dea.date=vac.date
where dea.continent is not NULL