select distinct * into #tmp from tablenamedrop table tablenameselect
* into tablename from #tmpdrop table #tmp

要是该表须要删除重复的记录(重复记录保留1条),能够按以下办法删除
select distinct * into #tmp from tablename
drop table tablename
select * into tablename from #tmp
drop table #tmp

比如现成一张人士表(表名:Person),若想将姓名、身份ID号、住址那多少个字段完全相像的笔录查寻找来,使用

   1:  SELECT p1.*   

   2:  FROM persons   p1,persons   p2   

   3:  WHERE p1.id<>p2.id   

   4:  AND p1.cardid   =   p2.cardid   

   5:  AND p1.pname   =   p2.pname   

   6:  AND p1.address   =   p2.address

能够兑现该意义。

删除重复记录的SQL语句

1.用rowid方法

2.用group by方法

3.用distinct方法

1。用rowid方法

据据oracle带的rowid属性,进行判定,是还是不是留存重复,语句如下:
查数据:      select * from table1 a where rowid !=(select   max(rowid)  
     from table1 b where a.name1=b.name1 and a.name2=b.name2……)
删数据:
    delete   from table1 a where rowid !=(select   max(rowid)  
     from table1 b where a.name1=b.name1 and a.name2=b.name2……)

2.group by方法

查数据:
select count(numState of Qatar, max(name卡塔尔(قطر‎ from student
–列出双重的记录数,并列出她的name属性
group by num
having count(num卡塔尔 >1
–按num分组后找寻表中num列重复,即现身次数超过叁遍
删数据:
delete from student
group by num
having count(num) >1
那样的话就把装有重复的都剔除了。

3.用distinct方法 -对于小的表相比较有用

create table table_new as   select distinct *   from table1 minux
truncate table table1;
insert into table1 select * from table_new;

询问及删除重复记录的方式大全
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 peopleId  in (select  peopleId  from people  group  by 
peopleId   having  count(peopleId) > 1)
and rowid not in (select min(rowid) from  people  group by peopleId 
having count(peopleId )>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)

(二卡塔尔 比方说 在A表中设有一个字段“name”,
还要分化记录之间的“name”值有超大可能会相仿,
至今正是须求查询出在该表中的各记录之间,“name”值存在双重的项;
Select Name,Count(*) From A Group By Name Having Count(*) > 1

固然还查性别也一模一样大则如下:
Select Name,sex,Count(*) From A Group By Name,sex Having Count(*)
> 1

(三) 方法一

declare @max integer,@id integer

declare cur_rows cursor local for select 主字段,count(*) from 表名
group by 主字段 having count(*) >; 1

open cur_rows

fetch cur_rows into @id,@max

while @@fetch_status=0

begin

select @max = @max -1

set rowcount @max

delete from 表名 where 主字段 = @id

fetch cur_rows into @id,@max

end

close cur_rows

set rowcount 0

方法二

"重复记录"有多个意思上的重复记录,一是完全重复的笔录,也即怀有字段均再度的记录,二是一对首要字段重复的笔录,譬喻Name字段重复,而其它字段不必然再次或都再也能够忽视。

1、对于第一种重复,相比较便于化解,使用

select distinct * from tableName

就能够获得无重复记录的结果集。

一经该表需求删除重复的记录(重复记录保留1条),能够按以下方式删除

select distinct * into #Tmp from tableName

drop table tableName

select * into tableName from #Tmp

drop table #Tmp

产生这种重新的来由是表设计不周发生的,扩充独一索引列就可以肃清。

2、那类重复难题普通必要保存重复记录中的第一条记下,操作方法如下

若是有重复的字段为Name,Address,供给得到那七个字段独一的结果集

select identity(int,1,1) as autoID, * into #Tmp from tableName

select min(autoID) as autoID into #Tmp2 from #Tmp group by
Name,autoID

select * from #Tmp where autoID in(select autoID from #tmp2)

说起底多少个select即取得了Name,Address不重复的结果集(但多了多个autoID字段,实际写时得以写在select子句中节省此列)

(四卡塔尔(قطر‎ 查询重复

select * from tablename where id in (

select id from tablename

group by id

having count(id) > 1)

–查看结果select * from #t/*num name———– —-1 a2 b

删除重复数据sql语句
方法一

方法1:

方法二
declare @max integer,@id integer
declare cur_rows cursor local for select 主字段,count(*) from 表名
group by 主字段 having count(*) > 1
open cur_rows
fetch cur_rows into @id,@max
while @@fetch_status=0
begin
select @max = @max -1
set rowcount @max
delete from 表名 where 主字段 = @id
fetch cur_rows into @id,@max
end
close cur_rows
set rowcount 0

–查看结果select * from #t

方法一
假若有重复的字段为name,address,供给取得这八个字段独一的结果集 select
identity(int,1,1卡塔尔(قطر‎ as autoid, * into #tmp from t…

sql 删除重复记录未有大小关系时,管理重复值

方法三

declare @max integer,@id integerdeclare cur_rows cursor local for
select 主字段,count(*) from 表名 group by 主字段 having count(*)
1open cur_rowsfetch cur_rows into @id,@maxwhile
@@fetch_status=0beginselect @max = @max -1set rowcount @maxdelete
from 表名 where 主字段 = @idfetch cur_rows into @id,@maxendclose
cur_rowsset rowcount 0

删除从前先用select语句查看要被删去的数额
select *
from 表 a
where exists(select * from 表 where  花费项目=a.花费档案的次序 and
时间>=dateadd(minute,-2,a.时间State of Qatar and 时间<a.时间卡塔尔国

/*num name———– —-1 a2 b

实例

admin

相关文章

发表评论

电子邮件地址不会被公开。 必填项已用*标注