用 SOUNDEX() 比對姓名
像是把 'Illinois' 寫成 'Ilynois' 這樣的凌亂字串,會在分析資料時帶來問題。因此,事先偵測這些情況很重要。
你在分析 flight_statistics 資料表時發現,有些 statistician_name 和 statistician_surname 的寫法不同,例如 Miriam Smith 與 Myriam Smyth。你擔心還有更多類似差異,所以想要全面檢查這些姓名。
你打算用 SOUNDEX() 來比較統計人員的姓名。如果 SOUNDEX() 的結果相同,但實際文字不同,就能找出需要清理的資料。
本練習屬於課程
在 SQL Server 資料庫中清理資料
練習說明
- 從
S1選出statistician_name與statistician_surname欄位的不重複值。 - 以
SOUNDEX()比對發音相近的名字與姓氏,將flight_statistics資料表以S2的別名進行 INNER JOIN。 - 篩掉在
S1與S2中,statistician_name與statistician_surname各自彼此相同的值,只保留兩邊不同者。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
SELECT
-- First name and surname of the statisticians
DISTINCT S1.___, S1.___
-- Join flight_statistics with itself
FROM ___ S1 INNER JOIN ___ S2
-- The SOUNDEX result of the first name and surname have to be the same
ON ___(S1.___) = ___(S2.___)
AND ___(S1.___) = ___(S2.___)
-- The texts of the first name or the texts of the surname have to be different
WHERE S1.___ <> S2.___
OR S1.___ <> S2.___