sqlserver重复记录
① 如何使用sql语句在sqlserver中删除重复数据
题主可 参考下列例句:
删除表t1字段col1有重复的记录
delete from t1 where exists
(select 1 from (select col1 from t1 group by col1 having count(1)>1) t where t.col1=t1.col1);
如果希望对于有重复的记录希望保留其中一条记录而不是全部删除,则可以运行下列语句,前提是数据表必须含有自增id列。
delete from t1 where exists
(select 1 from (select col1,max(id) as id from t1 group by col1 having count(1)>1) t where t.col1=t1.col1 and t.id<>t1.id);
② 怎么查看数据库表中某个字段的值有哪些重复记录
下面以 sqlserver数据库为例进行说明。
select * from TableA where b in (select b from TableAgroup by b having count(b) > 1)
这样就列举出了b字段所有的重复数据,可以根据对应的行号,取得位于第几行。
如果要查询a字段或者c字段重复数据,可以相应的把上面的b字段替换成a字段或c字段即可。
举例:
1、创建表student
这样就查出名字重复列,以及行号id。
(2)sqlserver重复记录扩展阅读:
1. sqlserver其他相关的一些查询:
(1)删除表中多余的重复记录,重复记录是根据单个字段(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)
(2)查找表中多余的重复记录(多个字段)
select * from vitae a where (a.peopleId,a.seq) in
(select peopleId,seq from vitae group by peopleId,seq having count(*) > 1)
(3)查找表中多余的重复记录(多个字段),不包含rowid最小的记录
select * from vitae a where (a.peopleId,a.seq) in
(select peopleId,seq from vitae group by peopleId,seq havingcount(*) > 1) and
rowid not in (select min(rowid) from vitae group by peopleId,seq having count(*)>1)
2. SQL语言元素
1、子句,是语句和查询的组成部分。
2、表达式,可以生成标量值,也可以生成由列和行数据组成的表。
3、谓词,指定可以评估为SQL三值逻辑(3VL)(真/假/未知)或布尔真值的条件,用于限制语句和查询的效果,或用于更改程序流。
4、查询,根据特定条件检索数据。这是SQL的一个重要元素。
语句可能对架构和数据产生持久影响,或者可能控制事务,程序流,连接,会话或诊断。
SQL语句还包括分号(“;”)语句终止符。虽然并非每个平台都需要,但它被定义为SQL语法的标准部分。在SQL语句和查询中通常会忽略无关紧要的空格,从而可以更轻松地格式化SQL代码以提高可读性。
③ sqlserver 的表数据录入重复怎么删,保留一份
别直接导到正式表,可以先导到临时表
如正式表AA,临时表AA_temp
先导到AA_temp,再用语句导到AA,
insert into AA select 列1,列2,…… from AA_temp group by 列1,列2,……
也可以加个where not in ()
④ 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视图中怎么处理重复数据
distinct 一下就可以了
⑥ sqlserver 如何横向刷新重复数据
示例,创建数据表stuinfo,有三个字段recno(自增),stuid,stuname:
CREATE TABLE [StuInfo] ([recno] [int] IDENTITY (1, 1) NOT NULL ,[stuid] [varchar] (10) COLLATE Chinese_PRC_CI_AS NOT NULL ,[stuname] [varchar] (10) COLLATE Chinese_PRC_CI_AS NOT NULL) ON [PRIMARY]GO
一、查某一列(或多列)的重复值。(只可以查出重复记录的值,不能查出整个记录的信息)
例如:查找stuid,stuname重复的记录:
select stuid,stuname from stuinfogroup by stuid,stunamehaving(count(*))>1
二、查某一列有重复值的记录。(此方法查出的是所有重复的记录,如果有两条记录重复的,就查出两条)
例如:查找stuid重复的记录:
select * from stuinfowhere stuid in (select stuid from stuinfogroup by stuidhaving(count(*))>1)
三、查某一列有重复值的记录。(只显示多余的记录,也就是说如果有三条记录重复的,就显示两条)
前提:需有一个不重复的列,此示例为recno。例如:查找stuid重复的记录:
select * from stuinfo s1where recno not in (select max(recno) from stuinfo s2where s1.stuid=s2.stuid
⑦ 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 去掉重复记录
首先设定表tb_a 唯一关键字段 xh,以及要查询的重复字段 mc 则查询mc重复的sqlserver语句如下
select mc from tb_a where xh not in (select min(xh) xh from tb_a group by mc)
⑨ SQLserver数据库中所有字段全部一样的重复数据如何删除
找到最大的rowid即可。
Sql代码:
alterprocgetNotDupData
as
--cleartemptable
deleteODS.dbo.Agent
deletefromstage.dbo.tmpDup
deletefromstage.dbo.tmpRowNo
deletefromstage.dbo.tmpMaxRowNo
--createptable
insertintostage.dbo.tmpDup
selectdistinctAgentLogin,AgentSurName,AgentGivenNamefromstage.dbo.dAgentPerformanceStat
'3%'orderbyAgentLogin
--addrowNo
insertintotmpRowNo
select*,ROW_NUMBER()over(orderbyAgentLogin)asrownofromtmpDup
--getmaxrowno
insertintostage.dbo.tmpMaxRowNo
selectmax(rowno)as'rowno'fromstage.dbo.(*)>1
--removemaxrowno
deletefromstage.dbo.tmpRowNowhererownoin(select*fromstage.dbo.tmpMaxRowNo)
--insertintoods
insertintoODS.dbo.AgentselectAgentLogin,AgentSurName,AgentGivenNamefromstage.dbo.tmpRowNo
⑩ SQLSERVER 怎样去除重复记录
distinct关键字
select distinct 姓名 from 表a
这条语句在显示时可以提取表a中的姓名,而且如果姓名重复的话,只显示一条,单并不对数据库中的数据产生影响,只是显示的时候重复的记录只显示一条