温度对充电速率的影响
您一直用窗口函数分析的数据,不仅对您的团队有帮助,对车辆制造商也很有价值。现在有多家厂商想了解温度对充电速率的影响。他们希望看到温度的移动平均值,以及按车型拆分的充电速率移动平均值。
本练习是课程的一部分
Snowflake 中的 Window Functions
练习说明
- 计算
temperature的移动平均值,基于按temperature排序下,使用当前行及其之前的 2 条记录。 - 再生成一个移动平均值,这次针对
charging_rate,同样使用当前行及其之前的 4 条记录,并按temperature排序、按vehicle_model分区。 - 仅包含
charging_rate不为NULL的记录。
交互式实操练习
通过完成这段示例代码来试试这个练习。
SELECT
vehicle_model,
charger_type,
temperature,
charging_rate,
-- Create a moving average of temperature using the two preceding and following records
___(___) OVER(
PARTITION BY vehicle_model
ORDER BY ___
ROWS BETWEEN ___ ___ AND ___ ___
) AS moving_average_temperature,
-- Find the moving average charging rate for the preceding four charging sessions
___(___) OVER(
PARTITION BY ___
ORDER BY ___
___
) AS moving_average_charging_rate
FROM ELECTRIC_VEHICLES.charging
-- Only include non-NULL charging rates
WHERE ___
ORDER BY vehicle_model, charger_type, temperature;