-
Notifications
You must be signed in to change notification settings - Fork 0
/
P3-CLimate_Data V3.sql
194 lines (152 loc) · 5.29 KB
/
P3-CLimate_Data V3.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
-- Drop the table if it exists
DROP TABLE IF EXISTS co2_emissions;
-- Create the table
CREATE TABLE co2_emissions (
entity varchar,
code varchar,
year numeric,
emissions numeric
);
SELECT *
FROM co2_emissions;
COPY co2_emissions (
entity,
code,
year,
emissions
)
FROM 'C:\Users\INBA6454\Documents\My SugarSync\Continuous Improvement\UCB Data Analytics Bootcamp\Lecture\Project3\Resources\FinalCLeanData\co2_emissions.csv'
DELIMITER ','
CSV HEADER;
-- Drop the table if it exists
DROP TABLE IF EXISTS wb_temp_anomaly;
-- Create the table
CREATE TABLE wb_temp_anomaly (
entity varchar,
year numeric,
avg_temp_anomaly numeric,
up_bound numeric,
lo_bound numeric
);
SELECT *
FROM wb_temp_anomaly;
-- Drop the table if it exists
DROP TABLE IF EXISTS temp_mo_1901_2021;
-- Create the table
CREATE TABLE temp_mo_1901_2021 (
Country varchar,
code varchar,
year numeric,
Jan varchar, Feb varchar, Mar varchar, Apr varchar,
May varchar, Jun varchar, Jul varchar, Aug varchar, Sep varchar,
Oct varchar, Nov varchar, Dec varchar
);
SELECT *
FROM temp_mo_1901_2021;
-- Tables created with wrong type
ALTER TABLE temp_mo_1901_2021
ALTER COLUMN Jan TYPE numeric USING Jan::numeric,
ALTER COLUMN Feb TYPE numeric USING Feb::numeric;
-- repeat for each month Mar to dec
-- Renamed COlumns to be more meaningful
ALTER TABLE temp_mo_1901_2021
RENAME COLUMN jan TO Jan_temp;
ALTER TABLE temp_mo_1901_2021
RENAME COLUMN Feb TO Feb_temp;
ALTER TABLE temp_mo_1901_2021
RENAME COLUMN Mar TO Mar_temp;
ALTER TABLE temp_mo_1901_2021
RENAME COLUMN Apr TO Apr_temp;
ALTER TABLE temp_mo_1901_2021
RENAME COLUMN May TO May_temp;
ALTER TABLE temp_mo_1901_2021
RENAME COLUMN Jun TO Jun_temp;
ALTER TABLE temp_mo_1901_2021
RENAME COLUMN Jul TO Jul_temp;
ALTER TABLE temp_mo_1901_2021
RENAME COLUMN Aug TO Aug_temp;
ALTER TABLE temp_mo_1901_2021
RENAME COLUMN Sep TO Sep_temp;
ALTER TABLE temp_mo_1901_2021
RENAME COLUMN Oct TO Oct_temp;
ALTER TABLE temp_mo_1901_2021
RENAME COLUMN Nov TO Nov_temp;
ALTER TABLE temp_mo_1901_2021
RENAME COLUMN Dec TO Dec_temp;
-- One more table for lat lon per country
-- Drop the table if it exists
DROP TABLE IF EXISTS country_lat_lon;
-- Create the table
CREATE TABLE country_lat_lon(
country varchar,
country_code varchar,
latitude numeric,
longitude numeric
);
SELECT *
FROM country_lat_lon;
COPY country_lat_lon(
country,
country_code,
latitude,
longitude
)
FROM 'C:\Users\INBA6454\Documents\My SugarSync\Continuous Improvement\UCB Data Analytics Bootcamp\Lecture\Project3\Resources\FinalCLeanData\country_lat_lon.csv'
DELIMITER ','
CSV HEADER;
-- Querries
-- average temperature per year
SELECT year, country,
(jan_temp + feb_temp + mar_temp + apr_temp + may_temp + jun_temp + jul_temp + aug_temp + sep_temp + oct_temp + nov_temp + dec_temp) / 12 AS avg_temperature
FROM temp_mo_1901_2021
GROUP BY country, year, jan_temp, feb_temp, mar_temp, apr_temp, may_temp, jun_temp, jul_temp, aug_temp, sep_temp, oct_temp, nov_temp, dec_temp
ORDER BY Year, country;
-- Comparing CO2 emissions over years
SELECT entity, year, SUM(emissions) as total_emissions
FROM co2_emissions
WHERE year BETWEEN 2000 AND 2021
GROUP BY entity, year
ORDER BY total_emissions, year;
-- Average temperature season per year
SELECT year, country,
(apr_temp + may_temp + jun_temp)/3 as spring_temp,
(jul_temp + aug_temp + sep_temp)/3 AS summer_temp,
(oct_temp + nov_temp + dec_temp)/3 AS fall_temp,
(jan_temp + feb_temp + mar_temp)/3 AS winter_temp
FROM temp_mo_1901_2021
GROUP BY country, year, jan_temp, feb_temp, mar_temp, apr_temp, may_temp, jun_temp, jul_temp, aug_temp, sep_temp, oct_temp, nov_temp, dec_temp
ORDER BY Year, country;
-- Temp & CO2 emissions per country per year
SELECT co2_emissions.entity AS country, co2_emissions.year, co2_emissions.emissions,\
(temp_mo_1901_2021.jan_temp + temp_mo_1901_2021.feb_temp + temp_mo_1901_2021.mar_temp + temp_mo_1901_2021.apr_temp + temp_mo_1901_2021.may_temp + temp_mo_1901_2021.jun_temp + temp_mo_1901_2021.jul_temp + temp_mo_1901_2021.aug_temp + temp_mo_1901_2021.sep_temp + temp_mo_1901_2021.oct_temp + temp_mo_1901_2021.nov_temp + temp_mo_1901_2021.dec_temp) / 12 AS avg_temperature\
FROM co2_emissions\
JOIN temp_mo_1901_2021 ON co2_emissions.entity = temp_mo_1901_2021.country AND co2_emissions.year = temp_mo_1901_2021.year\
ORDER BY co2_emissions.entity, co2_emissions.year;
-- Average temp per country and per year
SELECT year, country,
(jan_temp + feb_temp + mar_temp + apr_temp + may_temp + jun_temp + jul_temp + aug_temp + sep_temp + oct_temp + nov_temp + dec_temp) / 12 AS avg_temperature
FROM temp_mo_1901_2021
WHERE year > 1999
GROUP BY country, year,
jan_temp, feb_temp, mar_temp, apr_temp, may_temp, jun_temp, jul_temp, aug_temp, sep_temp, oct_temp, nov_temp, dec_temp
ORDER BY country, Year ASC;
-- Drop the table if it exists
DROP TABLE IF EXISTS climate_data;
-- Create the table
CREATE TABLE climate_data (
date timestamp,
Year numeric,
Location varchar(255),
country varchar(255),
Temperature numeric,
CO2_Emissions numeric,
Sea_Level_Rise numeric,
Precipitation numeric,
Humidity numeric,
Wind_Speed numeric
);
SELECT *
FROM climate_data;
--IMPORT CSV DATA FROM RESOURCES FOLDER
SELECT *
FROM climate_data;