-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDataExploration.sql
More file actions
145 lines (120 loc) · 4.99 KB
/
DataExploration.sql
File metadata and controls
145 lines (120 loc) · 4.99 KB
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
-- Checking Data collected is correct
select * from Projects..Deaths order by 3,4
select * from Projects..Vaccinations order by 3,4
-- Selecting Data(Cases) for particular country
select location, date , population, total_cases, new_cases
from Projects..Deaths where location = 'India'
select location, date , population, total_cases, new_cases
from Projects..Deaths where location like '%Ken%'
-- Total Cases vs Total Deaths
select location, date, population, total_cases, total_deaths , (total_deaths/total_cases)*100 as DeadPercentage
from Projects..Deaths
where continent is not null and location like 'Aus%'
order by DeadPercentage desc
-- Total Cases vs Population
select location, date, population, total_cases, (total_cases/population)*100 as InfectedPercentage
from Projects..Deaths
where continent is not null and location like 'Can%'
order by InfectedPercentage desc
-- Countries with Highest Infection rate
select location, population,max(total_cases) as HighCount, max((total_cases/population))*100 as InfectedPercentage
from Projects..Deaths
--where continent is not null
group by location , population
order by InfectedPercentage desc
-- Countries with Highest Death Rate
select location, max(cast(total_deaths as int)) as HighDeathCount, max((cast(total_deaths as int)/population))*100 as HighDeathPercentage
from Projects..Deaths
where continent is not null
group by location
order by HighDeathCount desc
-- Continents with Highest Death Rate
select continent , max(cast(total_deaths as int)) as HighDeathCount, max((cast(total_deaths as int)/population))*100 as HighDeathPercentage
from Projects..Deaths
where continent is not null
group by continent
order by HighDeathCount desc
-- Total Cases & Total Deaths per day all over globe
select date, sum(new_cases)as CaseTot, sum(cast(new_deaths as int)) as DeathTot , (sum(cast(new_deaths as int))/sum(new_cases))*100 as DP
from Projects..Deaths
where continent is not null
-- where location like 'Aus%'
group by date
order by 1,2
-- Joining two tables
select * from Projects..Deaths d
join Projects..Vaccinations v
on d.location = v.location
and d.date = v.date
order by 3,4
-- Total Population vs Vaccinated People
select d.continent , d.location , d.date , d.population , v.new_vaccinations from Projects..Deaths d
join Projects..Vaccinations v
on d.location = v.location
and d.date = v.date
where d.continent is not null
order by 1,2,3
select d.continent , d.location , d.date , d.population , v.new_vaccinations ,
SUM(CONVERT(int,v.new_vaccinations)) OVER (Partition by d.Location Order by d.location, d.Date) as RollingPplVaccinated
from Projects..Deaths d
join Projects..Vaccinations v
on d.location = v.location
and d.date = v.date
where d.continent is not null
order by 2,3
-- Use CTE
with PopvsVac(Continent, Location, Date, Population, New_Vaccinations , RollingVaccinated)
as(
select d.continent , d.location , d.date , d.population , v.new_vaccinations ,
SUM(CONVERT(int,v.new_vaccinations)) OVER (partition by d.location order by d.location,d.date)
as RollingVaccinated
from Projects..Deaths d
join Projects..Vaccinations v
on d.location = v.location
and d.date = v.date
where d.continent is not null
--order by 2,3
)
select * , (RollingVaccinated/Population)*100 from PopvsVac
-- Temporary table
drop table if exists PPC
Create table PPC (
Continent nvarchar(255),
Location nvarchar(255),
Date datetime,
Population numeric,
new_vacc numeric,
RollingVaccinated numeric)
insert into PPC
select d.continent , d.location , d.date , d.population , v.new_vaccinations ,
SUM(CONVERT(int,v.new_vaccinations)) OVER (partition by d.location order by d.location,d.date)
as RollingVaccinated
from Projects..Deaths d
join Projects..Vaccinations v
on d.location = v.location
and d.date = v.date
--where d.continent is not null
--order by 2,3
select * , (RollingVaccinated/Population)*100 from PPC
-- Create View as for visulizations
Create View PercentPopulationVaccinated as
Select dea.continent, dea.location, dea.date, dea.population, vac.new_vaccinations
, SUM(CONVERT(int,vac.new_vaccinations)) OVER (Partition by dea.Location Order by dea.location, dea.Date) as RollingPeopleVaccinated
--, (RollingPeopleVaccinated/population)*100
From Projects..Deaths dea
Join Projects..Vaccinations vac
On dea.location = vac.location
and dea.date = vac.date
where dea.continent is not null
Create view TotalCases_Deaths_per_day as
select date, sum(new_cases)as CaseTot, sum(cast(new_deaths as int)) as DeathTot , (sum(cast(new_deaths as int))/sum(new_cases))*100 as DP
from Projects..Deaths
where continent is not null
-- where location like 'Aus%'
group by date
--order by 1,2
create view totalcasesvstotaldeaths as
select location, date, population, total_cases, total_deaths , (total_deaths/total_cases)*100 as DeadPercentage
from Projects..Deaths
where continent is not null and location like 'Aus%'
--order by DeadPercentage desc