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.首先,运行上面的 GROUP BY 查询来确定有多少组重复的 PK 值及每组的重复数。 2.选择重复的键值放入临时表中。例如: SELECTcol1,col2,col3=count(*) INTOholdkey FROMt1 GROUPBYcol1,col2 HAVINGcount(*)1 3.选择重复的行放入临时表中,以清除进程中的重复值。例如: SELECTDISTINCTt1.* INTOholdps FROMt1,holdkey WHEREt1.col1=holdkey.col1 ANDt1.col2=holdkey.col2 SELECTcol1,col2,count(*) FROMholdps GROUPBYcol1,col2 5.从原始表中删除重复的行。例如: DELETEt1 FROMt1,holdkey WHEREt1.col1=holdkey.col1 ANDt1.col2=holdkey.col2 6.将唯一行放回原始表中。例如:
⑶ Sql Server表里面有2行数据完全一样,如何删除
Sql
Server里面如果没有设定主键而删除重复数据很麻烦:
一:保留重复记录中的一条记录,其他全部删除。
--1:建立临时表,把不重复的数据转存
select
distinct
*
into
#Tmp
from
表名;
--2:删除原表数据
truncate
table
表名;
--3:将数据导回
insert
into
表名
select
*
from
#Tmp;
--4:删除临时表
drop
table
#Tmp;
二:删除全部重复记录:
delete
from
表名
where
字段1=xxx,字段2=xxx....(把你重复记录的条件列在这里)
---
以上,希望对你有所帮助。
⑷ SQL查询,如何去除重复的记录
首先,先说明一个问题。这样的结果出现,说明系统设计是有问题的。
其次
删除重复数据,你要提供你是什么数据库。
不同数据库会有不同的解决方案。
关键字Distinct 去除重复,如下列SQL,去除Test相同的记录;
1. select distinct Test from Table
2. 如果是要删除表中存在的重复记录,那就逻辑处理,如下:
3. select Test from Table group by Test having count(test)>1
4. 先查询存在重复的数据,后面根据条件删除
还有一个更简单的方法可以尝试一下:
select aid, count(distinct uid) from 表名 group by aid
这是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删除重复记录
delete from friend where id in (select id from
(select id,userid, row_number() over(partition by userid order by id) as rn from friend ) as a where rn > 1)
⑺ 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
⑻ mysql,sqlserver数据库去重
b. 方法:
☆根据dname分组,查找出deptno最小的。然后再查找deptno不包含刚才查出来的。这样就查询出了所有的重复数据(除了deptno最小的那行)
方法2
删除重复的行
单个字段的如果会了,多个字段也非常简单。就是将group by 的字段增加为你想要的即可。
此处只写一个,其他方法请仿照一个字段的写即可。
查询结果不含指定字段重复
2.表需要删除重复的记录(重复记录保留1条),
3.查询重复
4.1、查找表中多余的重复记录,重复记录是根据单个字段(peopleId)来判断
4.2、删除表中多余的重复记录,重复记录是根据单个字段(peopleId)来判断,只留有rowid最小的记录
4.3、查找表中多余的重复记录(多个字段)
4.4、删除表中多余的重复记录(多个字段),只留有rowid最小的记录
4.5、查找表中多余的重复记录(多个字段),不包含rowid最小的记录
4.6.消除一个字段的左边的第一位:
4.7.消除一个字段的右边的第一位:
4.8.假删除表中多余的重复记录(多个字段),不包含rowid最小的记录
查询重复
⑼ sqlserver利用存储过程去除重复行的sql语句
还是先上代码吧
,可以先看
SQL语句去掉重复记录,获取重复记录
复制代码
代码如下:
ALTER
procere
[dbo].[PROC_ITEMMASTER_GETUNIQUE]
@PAGEINDEX
INT,@uid
int,@itemnumber
varchar(50)
AS
begin
tran
--开始事务
drop
table
[ItemMaster].[dbo].[testim]
--删除表
--把不重复记录转存到testim中
select
*
into
[ItemMaster].[dbo].[testim]
from
[ItemMaster].[dbo].[dat_item_master]
where
item_uid
in(select
min(item_uid)
as
item_uid
from
[ItemMaster].[dbo].[dat_item_master]
group
by
item_number)
and
status=0
select
top
10
*
from
[ItemMaster].[dbo].[testim]
where
item_uid
not
in
(select
top
(10*(@PAGEINDEX-1))
item_uid
from
[ItemMaster].[dbo].[testim])
and
owneruid=@uid
and
item_number
like
@itemnumber+'%'
--判断是否出错
if
@@error<>0
begin
rollback
tran
--出错则回滚
end
else
begin
--否则提前事务
commit
tran
end
我的数据是这样的:因为item_uid是标识列,item_number有重复的,
我想过滤成这样:
顺带说几个在编程的时候遇到的小问题
1.程序
出现
Could
not
find
stored
procere
找不到这个存储过程
因为我的程序数据库有四个,而默认连接是A,但实际要执行B库里的存储过程,导致出错,
解决办法1:可在A里面建个一样的存储过程2:在执行连接的时候,替换下数据库就行了
2.
asp.net/C#
将存储过程中返回的数据集,填充到dataset/datatable
复制代码
代码如下:
SqlConnection
conn
=
new
SqlConnection(ConfigurationManager.ConnectionStrings["SolutionSQLServer"].ToString());
SqlCommand
cmd
=
new
SqlCommand("Test",conn);
cmd.CommandType
=
CommandType.StoredProcere;
cmd.Parameters.Add("@MaxId",
SqlDbType.Int).Value
=
12000;
SqlDataAdapter
sda
=
new
SqlDataAdapter(cmd);
DataTable
dt
=
new
DataTable();
sda.Fill(dt);
在这感谢
http://www.cnblogs.com/liujuncm5/archive/2009/08/31/1557569.html
3.在存储过程里面,写SQL语句不能动态不加order
by
功能
比如
复制代码
代码如下:
--·@new_orderby
是传入参数,不能这样写
select
top
(10*(2-1))
item_uid
from
testim
order
by
@new_orderby
--执行这个的时候,SQL会出现
The
SELECT
item
identified
by
the
ORDER
BY
number
1
contains
a
variable
as
part
of
the
expression
identifying
a
column
position.
Variables
are
only
allowed
when
ordering
by
an
expression
referencing
a
column
name.
不过我找到解决办法,不过很麻烦,
http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=9328
(第二个回答用
'
sql
'进行连接)
http://databases.aspfaq.com/database/how-do-i-use-a-variable-in-an-order-by-clause.html (用case
end
也行)
4.
select
into
和
insert
into
select
两种复制文句
(这里感谢http://www.cnblogs.com/freshman0216/archive/2008/08/15/1268316.html)
1.INSERT
INTO
SELECT语句
语句形式为:Insert
into
Table2(field1,field2,...)
select
value1,value2,...
from
Table1
要求目标表Table2必须存在,由于目标表Table2已经存在,所以我们除了插入源表Table1的字段外,还可以插入常量。
2.SELECT
INTO
FROM语句
语句形式为:SELECT
vale1,
value2
into
Table2
from
Table1
要求目标表Table2不存在,因为在插入时会自动创建表Table2,并将Table1中指定字段数据复制到Table2中。
5.顺便复习下常用的SQL方法语句
复制代码
代码如下:
declare
@name
varchar(200)
--声明变量
set
@name='abcd;def'
--赋值
print
'exec
len
:'+Convert(varchar(10),Len(@name))
--convert(type,value)转换,Len(value)获取大小
print
'exec
charindex:'+Convert(varchar(10),CharIndex('e',@name))--CharIndex(find,value)
在value中查找find的位置
print
'not
replace:'+@name
print
'exec
replace:'+Replace(@name,';','')
--用replace替换
print
'exec
substring:'+Substring(@name,0,3)--用substring截取
print
@@RowCount
--返回上一行代码受影响的行数
作者:chenhuzi