Exercise 3
Create View for Aggregate Global Temperatures
In this exercise, you will use dbt to create a table called aggregate_global_temperatures
in the global_temperatures
schema based on the stg_global_temperatures
table in the global_temperatures
schema. This view will group the data by year and provide aggregated temperature values using the appropriate aggregation functions.
To create the view and extract the desired columns:
- Open your dbt project and navigate to the appropriate schema directory (e.g.,
models/global_temperatures
). - Create a new file named
aggregate_global_temperatures.sql
(or any desired name) in the schema directory. - In the
aggregate_global_temperatures.sql
file, write the dbt model definition for the view. - Define the
aggregate_global_temperatures
model using theview
materialization type. - Specify the columns for the view using the
select
statement. - In the
select
statement, include the following columns from thestg_global_temperatures
table:Date
LandAverageTemperature
LandMaxTemperature
LandMinTemperature
LandAndOceanAverageTemperature
- Extract the year from the
Date
column to obtain only the year portion of the date. - Apply suitable aggregation functions, such as
AVG
for average temperature, to calculate the desired values for each year. - Ignore any 'Uncertainty' columns for this project.
- Rename the columns as specified in the desired output, using the
as
keyword to assign meaningful aliases. - Save the file
aggregate_global_temperatures.sql
. - Run the dbt command to build the project and create the view in the database.
By following these instructions and executing the appropriate dbt commands, you will create a view called aggregate_global_temperatures
in the global_temperatures
schema.
The view will have the specified columns:
Year
(integer)LandAverageTemperature
(float)LandMaxTemperature
(float)LandMinTemperature
(float)LandAndOceanAverageTemperature
(float).