I have a table having person names address and job title. the names are repeating sometimes. I have to compare if two people have the same name and same address than i have to keep only 1 record of them.
我有一张有人名地址和职称的表。名字有时会重复。我必须比较两个人是否有相同的名字和相同的地址,而不是只保留1个记录。
Table: Data_Excel
Name: P_Name
Address: P_Address
City: P_city
2 个解决方案
#1
12
To find the duplicates you can do:
要查找重复项,您可以执行以下操作:
SELECT P_name,
P_Address,
P_city
FROM Data_Excel
GROUP BY P_Name,
P_Address,
P_city
HAVING COUNT(*) > 1;
To remove duplicates you could do:
要删除重复项,您可以执行以下操作:
DELETE
FROM Data_Excel
WHERE rowid NOT IN (
SELECT MIN(rowid)
FROM Data_Excel
GROUP BY P_Name,
P_Address,
P_city
);
To Insert in Person table you would do:
要插入人员表,您将执行以下操作:
INSERT INTO Person(id,name)
SELECT (SELECT MAX(id)+1 FROM Person),P_Name
FROM Data_Excel WHERE P_Name NOT IN (SELECT name FROM Person)
#2
2
SELECT P_Name,P_Address,count(*)
FROM Data_Excel
GROUP BY P_Name,P_Address
HAVING count(*) > 1;
This will give you the records with same P_Name
& P_Address
.
这将为您提供具有相同P_Name和P_Address的记录。
#1
12
To find the duplicates you can do:
要查找重复项,您可以执行以下操作:
SELECT P_name,
P_Address,
P_city
FROM Data_Excel
GROUP BY P_Name,
P_Address,
P_city
HAVING COUNT(*) > 1;
To remove duplicates you could do:
要删除重复项,您可以执行以下操作:
DELETE
FROM Data_Excel
WHERE rowid NOT IN (
SELECT MIN(rowid)
FROM Data_Excel
GROUP BY P_Name,
P_Address,
P_city
);
To Insert in Person table you would do:
要插入人员表,您将执行以下操作:
INSERT INTO Person(id,name)
SELECT (SELECT MAX(id)+1 FROM Person),P_Name
FROM Data_Excel WHERE P_Name NOT IN (SELECT name FROM Person)
#2
2
SELECT P_Name,P_Address,count(*)
FROM Data_Excel
GROUP BY P_Name,P_Address
HAVING count(*) > 1;
This will give you the records with same P_Name
& P_Address
.
这将为您提供具有相同P_Name和P_Address的记录。