使用 CTE 進行篩選
你接到一個新任務,要根據美國田納西州 Coffee County 在 12 月的氣溫(hourlydrybulbtemperature)讀值建立一系列查詢。你決定先建立只含 12 月資料的 CTE,這樣可以把心力放在主要查詢上,也更方便針對各個問題反覆調整。第一個查詢應該只回傳機場航站的例行逐小時氣象報告(FM-15)。
本練習屬於課程
Redshift 入門
練習說明
- 建立名為
december_readings的 CTE,從coffee_county_weather選取提供的欄位。 - 將日期篩選在
'2023-12-01'到'2024-12-31'之間。 - 從該 CTE 選取
date與air_temp欄位。 - 進一步只保留
report_type為'FM-15'的結果。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
-- Build a CTE named december_reading
___ ___ ___ (
SELECT date,
hourlydrybulbtemperature AS air_temp,
report_type
FROM public_intro_redshift.coffee_county_weather
-- Date is in December using between syntax
WHERE date ___ '2023-12-01' AND '2024-12-31'
)
SELECT date,
air_temp
-- From the CTE
FROM ___
-- For the FM-15 report type
WHERE ___ = 'FM-15';