使用 DIFFERENCE() 比较姓名
在上一个练习中,您使用了 SOUNDEX() 来检查 flight_statistics 表中统计员的姓名。
这一次,目标相似,但改用 DIFFERENCE() 函数。DIFFERENCE() 在两段字符串相同或非常相似时返回 4,在几乎没有相似度时返回 0。
如果两段字符串的 DIFFERENCE() 结果为 4,但它们的文本并不完全相同,您就能发现需要清理的数据。
本练习是课程的一部分
在 SQL Server 数据库中清洗数据
练习说明
- 从
S1中选择statistician_name和statistician_surname列的去重值。 - 将
flight_statistics表按S2作为别名进行内连接,在两表对应的名字与姓氏上,当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.___