-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBigQuery_Tables.sql
More file actions
202 lines (188 loc) · 4.87 KB
/
Copy pathBigQuery_Tables.sql
File metadata and controls
202 lines (188 loc) · 4.87 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
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
194
195
196
197
198
199
200
201
202
-- First we create the dataset
CREATE SCHEMA IF NOT EXISTS `weather_air_quality`
OPTIONS (
location = "US"
);
-- TimeDim table
CREATE OR REPLACE TABLE `weather_air_quality.TimeDim`
(
time_key INT64 NOT NULL,
full_date DATE NOT NULL,
year INT64 NOT NULL,
quarter INT64 NOT NULL,
month INT64 NOT NULL,
day INT64 NOT NULL,
hour INT64 NOT NULL,
day_of_week INT64 NOT NULL,
is_weekend BOOL NOT NULL,
is_holiday BOOL NOT NULL
)
PARTITION BY full_date
CLUSTER BY hour
OPTIONS(
description="Time dimension table for weather and air quality measurements"
);
-- LocationDim table
CREATE OR REPLACE TABLE `weather_air_quality.LocationDim`
(
location_key INT64 NOT NULL,
latitude FLOAT64 NOT NULL,
longitude FLOAT64 NOT NULL,
city STRING NOT NULL,
region STRING NOT NULL,
climate_zone STRING NOT NULL,
elevation FLOAT64 NOT NULL
)
CLUSTER BY city, region
OPTIONS(
description="Location dimension table for measurement locations"
);
-- AirQualityStatusDim table
CREATE OR REPLACE TABLE `weather_air_quality.AirQualityStatusDim`
(
status_key INT64 NOT NULL,
aqi_category STRING NOT NULL,
health_implications STRING NOT NULL,
cautionary_statement STRING NOT NULL,
color_code STRING NOT NULL
)
OPTIONS(
description="Air quality status classification and descriptions"
);
-- WeatherConditionDim table
CREATE OR REPLACE TABLE `weather_air_quality.WeatherConditionDim`
(
condition_key INT64 NOT NULL,
weather_code INT64 NOT NULL,
description STRING NOT NULL,
category STRING NOT NULL,
severity_level INT64 NOT NULL
)
OPTIONS(
description="Weather conditions classification and descriptions"
);
-- AirQualityFact table
CREATE OR REPLACE TABLE `weather_air_quality.AirQualityFact`
(
timestamp DATETIME NOT NULL,
location_key INT64 NOT NULL,
status_key INT64 NOT NULL,
pm2_5 FLOAT64,
pm10 FLOAT64,
carbon_monoxide FLOAT64,
nitrogen_dioxide FLOAT64,
sulphur_dioxide FLOAT64,
ozone FLOAT64,
us_aqi INT64,
aerosol_optical_depth FLOAT64,
dust FLOAT64,
uv_index FLOAT64
)
PARTITION BY DATE(timestamp)
CLUSTER BY location_key, status_key
OPTIONS(
description="Air quality measurements fact table"
);
-- WeatherFact table
CREATE OR REPLACE TABLE `weather_air_quality.WeatherFact`
(
timestamp DATETIME NOT NULL,
location_key INT64 NOT NULL,
condition_key INT64 NOT NULL,
severity_key INT64 NOT NULL,
season_key INT64 NOT NULL,
temperature_2m FLOAT64,
apparent_temperature FLOAT64,
precipitation FLOAT64,
snowfall FLOAT64,
wind_speed_10m FLOAT64,
wind_direction_10m INT64,
wind_gusts_10m FLOAT64,
relative_humidity_2m FLOAT64,
surface_pressure FLOAT64,
cloud_cover INT64,
cloud_cover_low INT64,
cloud_cover_mid INT64,
cloud_cover_high INT64,
visibility FLOAT64,
precipitation_probability FLOAT64
)
PARTITION BY DATE(timestamp)
CLUSTER BY location_key, severity_key
OPTIONS(
description="Enhanced weather measurements fact table"
);
-- Create HarmonizedData table with partitioning and clustering
CREATE OR REPLACE TABLE `atmo-flow.weather_air_quality.HarmonizedData`
(
-- Timestamp
timestamp TIMESTAMP NOT NULL,
location_key INT64 NOT NULL,
-- Weather Features
temperature_2m FLOAT64,
apparent_temperature FLOAT64,
precipitation FLOAT64,
wind_speed_10m FLOAT64,
wind_direction_10m INT64,
relative_humidity_2m FLOAT64,
surface_pressure FLOAT64,
cloud_cover INT64,
visibility FLOAT64,
-- Air Quality Features
pm2_5 FLOAT64,
pm10 FLOAT64,
carbon_monoxide FLOAT64,
nitrogen_dioxide FLOAT64,
sulphur_dioxide FLOAT64,
ozone FLOAT64,
us_aqi INT64,
aerosol_optical_depth FLOAT64,
dust FLOAT64,
uv_index FLOAT64,
-- Time Features
year INT64 NOT NULL,
month INT64 NOT NULL,
hour INT64,
day_of_week INT64,
is_weekend BOOL,
season STRING,
-- Location Features
latitude FLOAT64,
longitude FLOAT64,
elevation FLOAT64
)
PARTITION BY
DATE_TRUNC(timestamp, MONTH)
CLUSTER BY
location_key, year, month
OPTIONS(
description="Harmonized weather and air quality data for machine learning",
labels=[("domain", "environmental"), ("data_type", "harmonized")]
);
-- Create SeasonDim table
CREATE OR REPLACE TABLE `weather_air_quality.SeasonDim`
(
season_key INT64 NOT NULL,
season_name STRING NOT NULL,
start_month INT64 NOT NULL,
end_month INT64 NOT NULL,
avg_daylight_hours FLOAT64,
typical_temp_range STRING,
characteristic_weather STRING
)
OPTIONS(
description="Season dimension with detailed seasonal characteristics"
);
-- Create SeverityDim table
CREATE OR REPLACE TABLE `weather_air_quality.SeverityDim`
(
severity_key INT64 NOT NULL,
severity_level INT64 NOT NULL,
severity_name STRING NOT NULL,
description STRING NOT NULL,
recommended_actions STRING NOT NULL,
alert_level STRING NOT NULL
)
OPTIONS(
description="Shared severity dimension for weather and air quality conditions"
);