返回
基础
分类

(2 行受影响),删除重复数据sql语句

日期: 2020-04-29 04:14 浏览次数 : 159

必赢手机登录网址 ,sql 删除重复记录没有大小关系时,处理重复值

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

-- -- (roy)生成

假设有重复的字段为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)

if not object_id('tempdb..#t') is null drop table #tgocreate table #t([num] int,[name] nvarchar(1))insert #tselect 1,n'a' union allselect 1,n'a' union allselect 1,n'a' union allselect 2,n'b' union allselect 2,n'b'go

方法二
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

方法1:

方法三

if object_id('tempdb..#') is not null drop table #select distinct * into # from #t--排除重复记录结果集生成临时表#

truncate table #t--清空表

insert #t select * from # --把临时表#插入到表#t中

如果该表需要删除重复的记录(重复记录保留1条),可以按以下方法删除
select distinct * into #tmp from tablename
drop table tablename
select * into tablename from #tmp
drop table #tmp

--查看结果select * from #t

实例

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

vieworder  id号      打卡机     消费类型    时间        价钱
1           a1       1号机     早餐       2006-08-03 07:09:23.000         2
2           a1       1号机     早餐       2006-08-03 07:10:13.000         2
3           a1       1号机     早餐       2006-08-03 07:10:19.000         2
4           a1       1号机     午餐       2006-08-03 12:02:10.000         5
5           a2       1号机     午餐       2006-08-03 12:11:10.000         5
6           a2       1号机     午餐       2006-08-03 12:12:10.000         5

(2 行受影响)*/

代码

--重新执行测试数据后用方法2方法2:

delete from 表 a
where exists(select *(2 行受影响),删除重复数据sql语句。 from 表 where  消费类型=a.消费类型 and 时间>=dateadd(minute,-2,a.时间) and 时间<a.时间)

alter table #t add id int identity--新增标识列godelete a from #t a where exists(select 1 from #t where num=a.num and name=a.name and ida.id)--只保留一条记录goalter table #t drop column id--删除标识列

删除之前先用select语句查看要被删除的数据
select *
from 表 a
where exists(select * from 表 where  消费类型=a.消费类型 and 时间>=dateadd(minute,-2,a.时间) and 时间<a.时间)

--查看结果select * from #t

方法一 假设有重复的字段为name,address,要求得到这两个字段唯一的结果集 select identity(int,1,1) as autoid, * into #tmp from t...

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

(2 行受影响)

*/

--重新执行测试数据后用方法3方法3:

declare roy_cursor cursor local forselect count(1)-1,num,name from #t group by num,name having count(1)1declare @con int,@num int,@name nvarchar(1)open roy_cursorfetch next from roy_cursor into @con,@num,@namewhile @@fetch_status=0begin set rowcount @con; delete #t where num=@num and name=@name set rowcount 0; fetch next from roy_cursor into @con,@num,@nameendclose roy_cursordeallocate roy_cursor

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

(2 行受影响)*///利用存储过程

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 distinct * into #tmp from tablenamedrop table tablenameselect * into tablename from #tmpdrop table #tmp