平均値による代入(イミュテーション)
TripDistance が 0 という不正な値になっている YellowTripData のレコードに対して、平均値による代入(イミュテーション)を適用するストアドプロシージャを作成してください。平均乗車距離の変数は精度 18、スケール 4(小数点以下 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;