如何因数据输入错误而找到重复数据? 我想找到拥有相同PhoneNumber的客户。 我需要列出客户FirstName和LastName
有什么想法? 感谢
修改
当我运行此查询时:
Select FirstName, LastName, PhoneNumber
From Customer
我得到了这个结果:
Person One 12345
Person Two 23456
Third Person 34567
First Person 12345
Person Three 34567
解决方案
这是Bulat答案的一个版本,到目前为止是最好的。其他答案没有给出我需要的结果。
SELECT
c1.FirstName as FirstName1,
c1.LastName as LastName1,
c2.FirstName as FirstName2,
c2.LastName as LastName2,
c1.PhoneNumber
FROM
Customer c1 INNER JOIN
Customer c2 ON c1.Phone = c2.Phone AND c1.CustomerID < c2.CustomerID
答案 0 :(得分:3)
此查询会为您提供duplicata
select firstname, lastname
from customers
group by phonenumber, firstname, lastname
having count(phonenumber) > 1
答案 1 :(得分:1)
您需要通过电话号码字段将CUstomers表连接到自己:
SELECT
c1.Name as Name1,
c1.Surname as Surname1,
c2.Name as Name2,
c2.Surname as Surname2,
c.Phone
FROM
Customers c1 INNER JOIN
Customers c2 ON c1.Phone = c2.Phone AND c1.id < c2.id
注意:与其他答案不同,即使姓名和姓氏不匹配,也会返回具有相同电话号码的客户。
答案 2 :(得分:0)
SELECT phone_number
FROM customers
GROUP
BY phone_number
HAVING Count(*) > 1
SELECT *
FROM customers
INNER
JOIN (
SELECT phone_number
FROM customers
GROUP
BY phone_number
HAVING Count(*) > 1
) As duplicate_phone_numbers
ON duplicate_phone_numbers.phone_number = customers.phone_number