网站建设资讯

NEWS

网站建设资讯

sqlserver重复项,sql 重复字段

sqlserver怎么删除重复数据

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

SQLServer去重复查询,不删除重复数据

1、要有定位基准,也就是说,你的表必需要有一个不重复的键值,如果没有,请你给这个表加一个字段,将这个字段设为自增变量字段,建议为int类型,比如字段名可为“编码”。

2、查重复的数据:

select *from 表名 where 编码 in

(select 编码 from 表名 group by 编码 having count(1) = 2)

3、删除所有有重复的记录:

delete from 表名 where 

编码 in(select 编码 from 表名 group by 编码 having count(1) = 2)

4、删去重复的,只留下重复记录中编码最大的一条:

delete from 表名 where 

编码 in(select 编码 from 表名 group by 编码 having count(1) = 2) 

and 编码 not in (select max(编码)from 表名 group by 编码 having count(1) =2)

sqlserver 数据有重复怎么删除

1、必须保证表中有主键或者唯一索引,或者某列数据不能重复。只有这样,才可能使用一句SQL来实现。否则只能考虑其它办法。下面的语句,假定BB列是不重复的,删除后保存BB列值最大的那条记录。

delete

from

where

aa

in

(select

aa

from

group

by

aa

having

count(aa)

1)

and

bb

not

in

(select

max(bb)

from

group

by

aa

having

count(aa)

1);

2、有多种写法:

delete

A

from

B

where

A.AA

=

B.AA

delete

A

from

A,B

where

A.AA

=

B.AA

delete

A

where

AA

in

(select

AA

from

B)

3、使用into关键字:

select

*

into

新表名

from

原表

4、取数据前3位,字段必须是类似char类型,使用类似substring这样的函数(SYBASE是substring,ORACLE是substr):

select

substring(字段,1,3)

from

表名


网站标题:sqlserver重复项,sql 重复字段
新闻来源:http://cdweb.net/article/dscoopj.html