均值插补
创建一个存储过程,对 YellowTripData 中 TripDistance 错误为 0 的记录执行均值插补。平均行程距离变量的精度应为 18,且保留 4 位小数。
本练习是课程的一部分
SQL Server 中的函数与存储过程编写
练习说明
- 创建名为
cuspImputeTripDistanceMean的存储过程。 - 创建一个数值变量:
@AvgTripDistance。 - 计算所有
TripDistance大于0的记录的平均TripDistance。 - 更新
YellowTripData中TripDistance为0的记录,并将其设为@AvgTripDistance。
交互式实操练习
通过完成这段示例代码来试试这个练习。
-- Create the stored procedure
___ ___ dbo.___
AS
BEGIN
-- Specify @AvgTripDistance variable
___ ___ AS numeric (18,4)
-- Calculate the average trip distance
___ ___ = ___(___)
FROM YellowTripData
-- Only include trip distances greater than 0
___ ___ ___ ___
-- Update the records where trip distance is 0
___ ___
___ ___ = ___
WHERE ___ = _
END;