使用 DIFFERENCE() 比對姓名
在前一個練習中,你使用 SOUNDEX() 檢查 flight_statistics 資料表中統計員的姓名。
這次你要做類似的事,但改用 DIFFERENCE() 函式。當兩個字串相同或非常相似時,DIFFERENCE() 會回傳 4;當幾乎沒有相似度時,會回傳 0。
如果兩個字串的 DIFFERENCE() 結果為 4,但實際文字不同,就代表你找到了需要清理的資料。
本練習屬於課程
在 SQL Server 資料庫中清理資料
練習說明
- 從
S1選取statistician_name與statistician_surname欄位的不重複值。 - 將
flight_statistics資料表以S2別名進行 INNER JOIN,條件為兩表中名字與姓氏的發音相近,亦即各自欄位之間的DIFFERENCE為 4。 - 濾除在
S1與S2中,statistician_name與statistician_surname各自不相同的那些紀錄。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
SELECT
-- First name and surnames of the statisticians
DISTINCT S1.___, S1.___
-- Join flight_statistics with itself
FROM ___ S1 INNER JOIN ___ S2
-- The DIFFERENCE of the first name and surname has to be equals to 4
ON ___(S1.___, S2.___) = 4
AND ___(S1.___, S2.___) = 4
-- The texts of the first name or the texts of the surname have to be different
WHERE S1.___ <> S2.___
OR S1.___ <> S2.___