การแทนค่าด้วยค่าเฉลี่ย (Mean Imputation)
สร้าง stored procedure เพื่อนำ mean imputation มาใช้กับระเบียนใน YellowTripData ที่มี TripDistance เป็นศูนย์ซึ่งเป็นค่าที่ไม่ถูกต้อง โดยตัวแปรระยะทางเดินทางเฉลี่ยควรมีความแม่นยำ 18 หลัก และทศนิยม 4 ตำแหน่ง
แบบฝึกหัดนี้เป็นส่วนหนึ่งของหลักสูตร
การเขียนฟังก์ชันและ Stored Procedures ใน SQL Server
คำแนะนำการฝึกหัด
- สร้าง stored procedure ชื่อ
cuspImputeTripDistanceMean - สร้างตัวแปรชนิด numeric:
@AvgTripDistance - คำนวณค่าเฉลี่ยของ
TripDistanceสำหรับทุกระเบียนที่TripDistanceมากกว่า0 - อัปเดตระเบียนใน
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;