全部綜合運用!
就是這個!現在該把你學到的一切拿來實戰。你公司的 CEO 想要一個儀表板,能深入檢視各個充電站據點的整體指標,包括像是耗用能源與充電成本等資料。在建立這個儀表板之前,你需要先建立一個資料集,把這些資訊整理出來。
本練習屬於課程
Snowflake 的 Window Functions
練習說明
- 針對特定
charging_station_location,依energy_consumed由大到小為每次充電工作提供排名。 - 依
charging_station_location產生charging_cost的「累計總和」。 - 建立一個視窗框架,使用「前兩筆」與「後兩筆」工作,計算各
charging_station_location的energy_consumed「移動平均」。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
SELECT
charging_station_location,
TO_DATE(charging_start_time),
charging_cost,
energy_consumed,
-- Provide a ranking for each charging session based on energy consumed, from greatest to least
___() OVER(
PARTITION BY charging_station_location
ORDER BY ___
) AS rank_energy_consumed,
-- Generate a "running total" of charging costs by charging station location
SUM(___) OVER(
___ ___ ___
ORDER BY charging_start_time
ROWS BETWEEN ___ ___ AND ___ ___
) AS running_total_charging_cost,
-- Build a window frame using the two preceding and two following sessions to find a moving average of energy consumed
___() OVER(
PARTITION BY ___
ORDER BY charging_start_time
ROWS BETWEEN ___ ___ AND ___ ___
) AS moving_average_energy_consumed
FROM ELECTRIC_VEHICLES.charging
WHERE energy_consumed IS NOT NULL
ORDER BY charging_station_location, charging_start_time;