最佳化的 GROUP BY 與 ORDER BY
在 GROUP BY 與 ORDER BY 中將欄位排在正確順序,對效能至關重要。當這些子句使用的是 SORTKEY 時,更是如此。
本練習屬於課程
Redshift 入門
練習說明
- 在 SELECT 子句中,正確地從日期欄位分別擷取年份與月份為獨立欄位。
- 以正確順序在語句中加入
ORDER BY欄位month與year。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
-- Extract the year and month from the date as separate elements
SELECT ___(___, date) as year,
___('month', ___) as month,
COUNT(
CASE WHEN hourlywindspeed != '0' THEN 1
ELSE NULL
END
)
FROM public_intro_redshift.coffee_county_weather
GROUP BY year, month
-- Add the ORDER BY fields in the proper order
ORDER BY ___ DESC, ___ DESC;