如何在表中查找重复的名称

时间:2013-11-20 10:59:10

标签: sql string duplicates oracle-sqldeveloper

我有一张有人名地址和职位的表。 名字有时会重复。我必须比较两个人是否有相同的名字和相同的地址,而不是只保留1个记录。

Table: Data_Excel
Name: P_Name
Address: P_Address
City:  P_city

2 个答案:

答案 0 :(得分:13)

要查找重复项,您可以执行以下操作:

SELECT P_name,
  P_Address,
  P_city
FROM Data_Excel
GROUP BY P_Name,
  P_Address,
  P_city
HAVING COUNT(*) > 1;

要删除重复项,您可以执行以下操作:

DELETE
FROM Data_Excel
WHERE rowid NOT IN (
    SELECT MIN(rowid)
    FROM Data_Excel
    GROUP BY P_Name,
      P_Address,
      P_city
    );

要插入人员表,您可以执行以下操作:

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)

答案 1 :(得分:2)

SELECT P_Name,P_Address,count(*)
FROM Data_Excel
GROUP BY P_Name,P_Address
HAVING count(*) > 1;

这将为您提供相同P_Name&的记录。 P_Address