select
字段1,字段2,字段3
from
table
group
by
字段1,字段2,字段3
having
count(*)>1
用上边这句能找出所有重复的数据
字段1,2,3你替换成你表里的字段名,如果有更多字段的话,你就继续添加,最后group
by的时候不要忘记了
删除的时候要建立一个临时表
create
table
new_table
as
select
字段1,字段2,字段3
from
old_table
group
by
字段1,字段2,字段3;
然后删除原表数据
truncate
table
old_table;
然后把临时表数据反插回去
insert
into
new_table
select
*
from
old_table;
2. SQL Server中怎样可以从SELECT语句的结果集中删除重复行
在要删除的有重复数据中存在几种情况:
1.存在两条完全相同的纪录
这是最简单的一种情况,用关键字distinct就可以去掉。
example: select distinct * from table(表名) where (条件)
2.存在部分字段相同的纪录(有主键id即唯一键)
如果是这种情况的话用distinct是过滤不了的,这就要用到主键id的唯一性特点及group by分组
example:
select * from table where id in (select max(id) from table group by [去除重复的字段名列表,....])
3.没有唯一键ID
example:
select identity(int1,1) as id,* into newtable(临时表) from table
select * from newtable where id in (select max(id) from newtable group by [去除重复的字段名列表,....])
(2)sql2008删除重复行扩展阅读:
SQL Server 是Microsoft 公司推出的关系型数据库管理系统。具有使用方便可伸缩性好与相关软件集成程度高等优点,可跨越从运行Microsoft Windows 98 的膝上型电脑到运行Microsoft Windows 2012 的大型多处理器的服务器等多种平台使用。
Microsoft SQL Server 是一个全面的数据库平台,使用集成的商业智能 (BI)工具提供了企业级的数据管理。Microsoft SQL Server数据库引擎为关系型数据和结构化数据提供了更安全可靠的存储功能,使您可以构建和管理用于业务的高可用和高性能的数据应用程序。
3. SQL消除结果集中的重复行
distinct
关键字可从
select
语句的结果中除去重复的行。如果没有指定
distinct,那么将返回所有行,包括重复的行。例如,如果在
titleauthor
中选择所有作者
id
时未使用
distinct,那么将会返回下列行(其中包括一些重复的行):
use
pubs
4. 如何删除 SQL Server 表中的重复行
一个最简单的方法,distinct去重复知道吧~用语句把所有去掉重复的记录查出来放进表A中,然后把表A的名字改成原来的,原来的删掉
5. SQL如何删除重复的数据行
SQL Server删除重复行是我们最常见的操作之一,下面就为您介绍六种适合不同情况的SQL Server删除重复行的方法,供您参考。
1.如果有ID字段,就是具有唯一性的字段
delect table tableName where id not in ( select max(id) from table group by col1,col2,col3... )
group by 子句后跟的字段就是你用来判断重复的条件,如只有col1,那么只要col1字段内容相同即表示记录相同。
2. 如果是判断所有字段也可以这样 ,【对于表中的指定的字段的进行检查是否相同】
select * into #temp from tablename group by id1,id2,....
delete tablename
insert into table select * from #temp
drop table #temp
3. 首先去重复,再获取N*1条数据插入到临时表中,【对于表中的所有字段的进行检查是否相同】,再将原表的数据删除,然后将临时表的数据插入到原表,最后删除临时表。
select distinct * into #temp from tablename
delete tablename
go
insert tablename select * from #temp
go
drop table #temp
4. 没有ID的情况
select identity(int,1,1) as id,* into #temp from tabel
delect # where id not in (
select max(id) from # group by col1,col2,col3...)
delect table
inset into table(...)
select ..... from #temp
5. col1+','+col2+','...col5 联合主键
select * from table where col1+','+col2+','...col5 in (
select max(col1+','+col2+','...col5) from table
where having count(*)>1
group by col1,col2,col3,col4
)
group by 子句后跟的字段就是你用来判断重复的条件,如只有col1,那么只要col1字段内容相同即表示记录相同。
6.
select identity(int,1,1) as id,* into #temp from tabel
select * from #temp where id in (
select max(id) from #emp where having count(*)>1 group by col1,col2,col3...)
6. sql server 2008如何将表中多余重复数据删除
你自己找到方法了,还问?
不过你这样写,效率会比较低,不能用索引。
你应该用exsist去写。
你试试样吧:
delete from OrderBaby ta where
exists
(
select count(*) as c from OrderBaby tb where ta.ObbbabyNo = tb.ObbbabyNo and ta.ObbOrderNo= tb.ObbOrderNo
having count(*) > 1
)
and ObbId not in (select min(ObbId) from OrderBaby group by ObbbabyNo+ObbOrderNo having count(*)>1)
7. SQL 语句去掉重复问题!
SQL>delete
cz
where
(id,forecid)
in
(select
id,forecid
from
table
group
by
id,forecid
having
count(*)>1)
and
rowid
not
in
(select
min(rowid)
from
table
group
by
id,forecid
having
count(*)>1);
SQL>delete
table
where
rowid
not
in(select
min(rowid)
from
cz
group
by
id,forecid
这两个方法都可以,适用于oracle删除大量重复数据!