SQL删除重复数据只保留一条

时间:2022-09-22 22:42:20
用SQL语句,删除掉重复项只保留一条

在几千条记录里,存在着些相同的记录,如何能用SQL语句,删除掉重复的呢
1、查找表中多余的重复记录,重复记录是根据单个字段(peopleId)来判断 
select * from people 
where peopleId in (select peopleId from people group by peopleId having count(peopleId) > 1) 

2、删除表中多余的重复记录,重复记录是根据单个字段(peopleId)来判断,只留有rowid最小的记录 
delete from people 
where   peopleName in (select peopleName    from people group by peopleName      having count(peopleName) > 1) 
and   peopleId not in (select min(peopleId) from people group by peopleName     having count(peopleName)>1) 

3、查找表中多余的重复记录(多个字段) 
select * from vitae a 
where (a.peopleId,a.seq) in (select peopleId,seq from vitae group by peopleId,seq having count(*) > 1) 

4、删除表中多余的重复记录(多个字段),只留有rowid最小的记录 
delete from vitae a 
where (a.peopleId,a.seq) in (select peopleId,seq from vitae group by peopleId,seq having count(*) > 1) 
and rowid not in (select min(rowid) from vitae group by peopleId,seq having count(*)>1) 

5、查找表中多余的重复记录(多个字段),不包含rowid最小的记录 
select * from vitae a 
where (a.peopleId,a.seq) in (select peopleId,seq from vitae group by peopleId,seq having count(*) > 1) 
and rowid not in (select min(rowid) from vitae group by peopleId,seq having count(*)>1)   

6.消除一个字段的左边的第一位:

update tableName set [Title]=Right([Title],(len([Title])-1)) where Title like '村%'

7.消除一个字段的右边的第一位:

update tableName set [Title]=left([Title],(len([Title])-1)) where Title like '%村'

8.假删除表中多余的重复记录(多个字段),不包含rowid最小的记录 
update vitae set ispass=-1
where peopleId in (select peopleId from vitae group by peopleId

============================================================================================

删除重复数据只保存一条--ms sql及oracle实现方法   ms sql方法:   declare @sss table (ID int,  Name varchar(10), address varchar(10)) insert into @sss select 1,'小王','重庆' insert into @sss select 2,'小张','北京' insert into @sss select 3,'小明','上海' insert into @sss select 4,'小红','重庆' insert into @sss select 5,'小李','重庆' insert into @sss select 6,'小白','北京' insert into @sss select 7,'小红','上海' insert into @sss select 8,'小红','重庆'    delete from @sss where id not in(select min(id) from @sss group by name,address) ------------------------------------------------------------------------------------------------------------ oracle方法:   SQL> CREATE TABLE test_delete(   2    name  varchar(10),   3    value INT   4  ); 表已创建。     SQL> INSERT INTO test_delete   2  SELECT '张三', 100 FROM dual   3  UNION ALL SELECT '张三', 100 FROM dual   4  UNION ALL SELECT '李四', 80  FROM dual   5  UNION ALL SELECT '王五', 80  FROM dual   6  UNION ALL SELECT '王五', 80  FROM dual   7  UNION ALL SELECT '赵六', 90  FROM dual   8  UNION ALL SELECT '赵六', 70  FROM dual; 已创建7行。   SQL> SELECT   2    ROWID,   3    name,   4    value   5  FROM   6    test_delete; ROWID              NAME            VALUE ------------------ ---------- ---------- AAAM2mAAGAAAAOXAAA 张三              100 AAAM2mAAGAAAAOXAAB 张三              100 AAAM2mAAGAAAAOXAAC 李四               80 AAAM2mAAGAAAAOXAAD 王五               80 AAAM2mAAGAAAAOXAAE 王五               80 AAAM2mAAGAAAAOXAAF 赵六               90 AAAM2mAAGAAAAOXAAG 赵六               70 已选择7行。
SQL> DELETE   2    test_delete   3  WHERE   4    (name, value)   5      IN (SELECT   6            name, value   7          FROM   8            test_delete   9          GROUP BY  10            name, value  11          HAVING COUNT(1) > 1)  12      AND rowid NOT IN  13         (SELECT  14            MIN(rowid)  15          FROM  16            test_delete  17          GROUP BY  18            name, value  19          HAVING  20            COUNT(1) > 1);   已删除2行。    SQL> SELECT   2    ROWID,   3    name,   4    value   5  FROM   6    test_delete;   ROWID              NAME            VALUE ------------------ ---------- ---------- AAAM2mAAGAAAAOXAAA 张三              100 AAAM2mAAGAAAAOXAAC 李四               80 AAAM2mAAGAAAAOXAAD 王五               80 AAAM2mAAGAAAAOXAAF 赵六               90 AAAM2mAAGAAAAOXAAG 赵六               70