Exercise 3
Create Table for Aggregate Global Temperatures
To analyze temperature measurements globally on an annual basis, you will 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 aggregate_global_temperatures
view based on the stg_global_temperatures
table in the global_temperatures
schema, and extract the desired columns while applying the appropriate aggregation functions, follow these steps:
- Select the
Date
,LandAverageTemperature
,LandMaxTemperature
,LandMinTemperature
, andLandAndOceanAverageTemperature
columns from thestg_global_temperatures
table. - Extract the year from the
Date
column to obtain only the year portion of the date. - Apply the
AVG
aggregation function to calculate the average values forLANDANDOCEANAVERAGETEMPERATURE
andLANDAVERAGETEMPERATURE
. - Apply the
MAX
aggregation function to calculate the maximum value forLANDMAXTEMPERATURE
. - Apply the
MIN
aggregation function to calculate the minimum value forLANDMINTEMPERATURE
. - Group the data by the
YEAR
column.
This view will group the temperature data by year and provide the aggregated values for LANDAVERAGETEMPERATURE
, LANDMAXTEMPERATURE
, LANDMINTEMPERATURE
, and LANDANDOCEANAVERAGETEMPERATURE
.
- YEAR: Integer
- LANDAVERAGETEMPERATURE: Float
- LANDMAXTEMPERATURE: Float
- LANDMINTEMPERATURE: Float
- LANDANDOCEANAVERAGETEMPERATURE: Float
Sample Output
YEAR | LANDAVERAGETEMPERATURE | LANDMAXTEMPERATURE | LANDMINTEMPERATURE | LANDANDOCEANAVERAGETEMPERATURE |
---|---|---|---|---|
1869 | 8.432083333 | 9.618 | -0.509 | 15.0655 |
1866 | 8.292166667 | 9.636 | -0.881 | 15.057 |
Note date column as the value like 2022-01-19 try to extract 2022 alone
Both Year and Date are Reserved Keyword
In the SQL statement, the use of double quotes around the YEAR column name is necessary because it is a reserved keyword in Snowflake.
select "YEAR" FROM TABLE