CSDN-CSDN社区-MS-SQL Server-应用实例

收藏 [推荐] 处理表重复记录(查询和删除)_整理贴4[问题点数:20,结帖人:roy_88]

  • roy_88
  • (中国风)
  • 等 级:
  • 结帖率:
  • 2

    4

楼主发表于:2008-06-26 00:16:03
SQL code
--处理表重复记录(查询和删除)
/******************************************************************************************************************************************************
1、Num、Name相同的重复值记录,没有大小关系只保留一条
2、Name相同,ID有大小关系时,保留大或小其中一个记录
整理人:中国风(Roy)

日期:2008.06.06
******************************************************************************************************************************************************/

--1、用于查询重复处理记录(如果列没有大小关系时2000用生成自增列和临时表处理,SQL2005用row_number函数处理)

--> --> (Roy)生成測試數據
 
if not object_id('Tempdb..#T') is null
	drop table #T
Go
Create table #T([ID] int,[Name] nvarchar(1),[Memo] nvarchar(2))
Insert #T
select 1,N'A',N'A1' union all
select 2,N'A',N'A2' union all
select 3,N'A',N'A3' union all
select 4,N'B',N'B1' union all
select 5,N'B',N'B2'
Go


--I、Name相同ID最小的记录(推荐用1,2,3),方法3在SQl05时,效率高于1、2
方法1:
Select * from #T a where not exists(select 1 from #T where Name=a.Name and ID=b.ID group by a.ID,a.Name,a.Memo having count(1)=1 

方法5:
select * from #T a group by ID,Name,Memo having ID=(select min(ID)from #T where Name=a.Name)

方法6:
select * from #T a where (select count(1) from #T where Name=a.Name and IDall(select ID from #T where Name=a.Name)

方法9(注:ID为唯一时可用):
select * from #T a where ID in(select min(ID) from #T group by Name)

--SQL2005:

方法10:
select ID,Name,Memo from (select *,min(ID)over(partition by Name) as MinID from #T a)T where ID=MinID

方法11:

select ID,Name,Memo from (select *,row_number()over(partition by Name order by ID) as MinID from #T a)T where MinID=1

生成结果:
/*
ID          Name Memo
----------- ---- ----
1           A    A1
4           B    B1

(2 行受影响)
*/


--II、Name相同ID最大的记录,与min相反:
方法1:
Select * from #T a where not exists(select 1 from #T where Name=a.Name and ID>a.ID)

方法2:
select a.* from #T a join (select max(ID)ID,Name from #T group by Name) b on a.Name=b.Name and a.ID=b.ID order by ID

方法3:
select * from #T a where ID=(select max(ID) from #T where Name=a.Name) order by ID

方法4:
select a.* from #T a join #T b on a.Name=b.Name and a.ID<=b.ID group by a.ID,a.Name,a.Memo having count(1)=1 

方法5:
select * from #T a group by ID,Name,Memo having ID=(select max(ID)from #T where Name=a.Name)

方法6:
select * from #T a where (select count(1) from #T where Name=a.Name and ID>a.ID)=0

方法7:
select * from #T a where ID=(select top 1 ID from #T where Name=a.name order by ID desc)

方法8:
select * from #T a where ID! --> (Roy)生成測試數據

if not object_id('Tempdb..#T') is null
	drop table #T
Go
Create table #T([ID] int,[Name] nvarchar(1),[Memo] nvarchar(2))
Insert #T
select 1,N'A',N'A1' union all
select 2,N'A',N'A2' union all
select 3,N'A',N'A3' union all
select 4,N'B',N'B1' union all
select 5,N'B',N'B2'
Go

--I、Name相同ID最小的记录(推荐用1,2,3),保留最小一条
方法1:
delete a from #T a where  exists(select 1 from #T where Name=a.Name and ID0

方法6:
delete a from #T a where ID<>(select top 1 ID from #T where Name=a.name order by ID)

方法7:
delete a from #T a where ID>any(select ID from #T where Name=a.Name)



select * from #T

生成结果:
/*
ID          Name Memo
----------- ---- ----
1           A    A1
4           B    B1

(2 行受影响)
*/


--II、Name相同ID保留最大的一条记录:

方法1:
delete a from #T a where  exists(select 1 from #T where Name=a.Name and ID>a.ID)

方法2:
delete a  from #T a left join (select max(ID)ID,Name from #T group by Name) b on a.Name=b.Name and a.ID=b.ID where b.Id is null

方法3:
delete a from #T a where ID not in (select max(ID) from #T where Name=a.Name)

方法4(注:ID为唯一时可用):
delete a from #T a where ID not in(select max(ID)from #T group by Name)

方法5:
delete a from #T a where (select count(1) from #T where Name=a.Name and ID>a.ID)>0

方法6:
delete a from #T a where ID<>(select top 1 ID from #T where Name=a.name order by ID desc)

方法7:
delete a from #T a where ID --> (Roy)生成測試數據
 
if not object_id('Tempdb..#T') is null
	drop table #T
Go
Create table #T([Num] int,[Name] nvarchar(1))
Insert #T
select 1,N'A' union all
select 1,N'A' union all
select 1,N'A' union all
select 2,N'B' union all
select 2,N'B'
Go

方法1:
if object_id('Tempdb..#') is not null
	drop table #
Select distinct * into # from #T--排除重复记录结果集生成临时表#

truncate table #T--清空表

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

--查看结果
select * from #T

/*
Num         Name
----------- ----
1           A
2           B

(2 行受影响)
*/

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

alter table #T add ID int identity--新增标识列
go
delete a from  #T a where  exists(select 1 from #T where Num=a.Num and Name=a.Name and ID>a.ID)--只保留一条记录
go
alter table #T drop column ID--删除标识列

--查看结果
select * from #T

/*
Num         Name
----------- ----
1           A
2           B

(2 行受影响)

*/

--重新执行测试数据后用方法3
方法3:
declare Roy_Cursor cursor local for
select count(1)-1,Num,Name from #T group by Num,Name having count(1)>1
declare @con int,@Num int,@Name nvarchar(1)
open Roy_Cursor
fetch next from Roy_Cursor into @con,@Num,@Name
while @@Fetch_status=0
begin 
	set rowcount @con;
	delete #T where Num=@Num and Name=@Name
	set rowcount 0;
	fetch next from Roy_Cursor into @con,@Num,@Name
end
close Roy_Cursor
deallocate Roy_Cursor

--查看结果
select * from #T
/*
Num         Name
----------- ----
1           A
2           B

(2 行受影响)
*/
回复次数:95
#1楼 得分:0回复于:2008-06-26 00:18:58
pubftxxyet tj udh kcg !
  • roy_88用户头像
  • roy_88
  • (中国风)
  • 等 级:
  • 2

    4

#2楼 得分:0回复于:2008-06-26 00:19:30
sf
---
后面欢迎大家补充~~~
#3楼 得分:1回复于:2008-06-26 09:00:59
啥意思?好多啊

我是2000的,看来就得用前面的方法了!
  • sweetweiwei用户头像
  • sweetweiwei
  • ((薇薇)世上本无事,庸人自扰之)
  • 等 级:
#4楼 得分:1回复于:2008-06-26 09:11:20
学习
#5楼 得分:1回复于:2008-06-26 09:22:26
学习
#6楼 得分:1回复于:2008-06-26 09:52:37
呵呵!楼主有整理知识的好习惯,值得学习!
其实搞过相关内容,没整理过。
以后也多整理下自己的知识,向楼主致敬!学习。
  • roy_88用户头像
  • roy_88
  • (中国风)
  • 等 级:
  • 2

    4

#7楼 得分:0回复于:2008-06-26 14:07:47
引用 6 楼 rockyvan 的回复:
呵呵!楼主有整理知识的好习惯,值得学习!
其实搞过相关内容,没整理过。
以后也多整理下自己的知识,向楼主致敬!学习。



沒有什麼好習慣,都是端午節花了一天時間整理了幾篇..沒貼出來
  • hery2002用户头像
  • hery2002
  • (苦*行*僧)
  • 等 级:
  • 2

    2

#8楼 得分:1回复于:2008-06-26 14:11:43
引用 7 楼 roy_88 的回复:
引用 6 楼 rockyvan 的回复:
呵呵!楼主有整理知识的好习惯,值得学习!
其实搞过相关内容,没整理过。
以后也多整理下自己的知识,向楼主致敬!学习。
沒有什麼好習慣,都是端午節花了一天時間整理了幾篇..沒貼出來

o
#9楼 得分:1回复于:2008-06-26 14:25:56
學習下謝謝了。
#10楼 得分:1回复于:2008-06-26 14:27:51
hao ya ,恩,不错
  • sdhylj用户头像
  • sdhylj
  • (青锋-SS)
  • 等 级:
  • 4

#11楼 得分:1回复于:2008-06-26 14:36:06
飞过.........
#12楼 得分:1回复于:2008-06-26 14:36:59
SQL code
select 
#13楼 得分:1回复于:2008-06-26 14:40:53
學習下謝謝了。
#14楼 得分:1回复于:2008-06-26 14:52:54
Support
Good thing
  • yan55667用户头像
  • yan55667
  • (逆流 · 逆游)
  • 等 级:
#15楼 得分:1回复于:2008-06-26 14:53:01
好贴  顶~~
#16楼 得分:1回复于:2008-06-26 15:23:47
引用 6 楼 rockyvan 的回复:
呵呵!楼主有整理知识的好习惯,值得学习!
其实搞过相关内容,没整理过。
以后也多整理下自己的知识,向楼主致敬!学习。


确实,这一点非常值得学习!
#17楼 得分:1回复于:2008-06-26 16:01:54
好贴,学习
#18楼 得分:1回复于:2008-06-26 16:06:01
引用 16 楼 IT_superman 的回复:
引用 6 楼 rockyvan 的回复:
呵呵!楼主有整理知识的好习惯,值得学习!
其实搞过相关内容,没整理过。
以后也多整理下自己的知识,向楼主致敬!学习。


确实,这一点非常值得学习!
  • aiyaoo用户头像
  • aiyaoo
  • (加紧学习)
  • 等 级:
#19楼 得分:1回复于:2008-06-26 16:53:18
收藏
#20楼 得分:0回复于:2008-06-26 16:55:16
yes
  • zzxap用户头像
  • zzxap
  • (风语者)
  • 等 级:
  • 2

#21楼 得分:1回复于:2008-06-26 17:26:13
Select * from #T a where not exists(select 1 from #T where Name=a.Name and ID <a.ID)
中的a是什么意思??
#22楼 得分:0回复于:2008-06-26 17:28:50
顶,,,
  • liuqian4243用户头像
  • liuqian4243
  • (新工作,新开始呀!为世界奋斗着)
  • 等 级:
#23楼 得分:1回复于:2008-06-26 17:58:20
学习        中。
#24楼 得分:1回复于:2008-06-26 19:28:30
學習了......
  • chyliu用户头像
  • chyliu
  • (chyliu)
  • 等 级:
#25楼 得分:1回复于:2008-06-26 19:32:08
别名为a
引用 21 楼 zzxap 的回复:
Select * from #T a where not exists(select 1 from #T where Name=a.Name and ID <a.ID)
中的a是什么意思??
#26楼 得分:0回复于:2008-06-26 20:11:17

SQL code
--求最大值
--条件:a值相同时,要b值大的,如果a值相同且存在两条或两条以上的数据的b值相同,则比较c值,取c值最大的数据,。。。以此类推
set nocount on 
if object_id('tempdb..#t') is not null drop table #t
create table #t ( a int,b int,c int)
insert into #t	select 1,2,1--〇
union all	select 1,1,2
------------------------------
union all	select 3,3,3
union all	select 3,3,4--〇
------------------------------
union all 	select 5,6,5
union all	select 5,4,6
union all	select 5,6,7--〇
set nocount off

select	* 
from	(select * from #t t where not exists(select 1 from #t where a=t.a and b>t.b)) m
where	exists (
	select	1 from (select * from #t t where 1>(select count(1) from #t where a=t.a and b>t.b)) n
	where	m.a=n.a 
	and	m.b=n.b 
	group	by n.a,n.b 
	having	m.c=max(n.c))

/*
select * from #t t where not exists(select 1 from #t where a=t.a and b>t.b)
与
select * from #t t where 1>(select count(1) from #t where a=t.a and b>t.b)
与
最外层的查询
三者功能相同
*/


风老大,
能不能给个好思路写出这个查询语句
上面这个用了个嵌套查询
但是如果还有D字段,也需要比较的话,就很麻烦了


#27楼 得分:0回复于:2008-06-26 20:20:54
各种牛X
拜读

收藏
学习
#28楼 得分:0回复于:2008-06-26 22:06:35
#29楼 得分:0回复于:2008-06-26 23:09:50
学习了
#30楼 得分:0回复于:2008-06-27 06:54:07
用distinct  可以筛选出不相同的字段.
#31楼 得分:0回复于:2008-06-27 08:38:03
情不自禁地赞中国风一个!
学习并收藏
#32楼 得分:0回复于:2008-06-27 09:43:41
收藏学习了
  • rczjp用户头像
  • rczjp
  • (I'm God of Gamb)
  • 等 级:
#33楼 得分:0回复于:2008-06-27 10:10:52
星星级别的还是牛点 顶个 呵呵
#34楼 得分:0回复于:2008-06-27 10:23:12
情不自禁地赞中国风一个!
学习并收藏
#35楼 得分:0回复于:2008-06-27 11:09:32
mark
#36楼 得分:0回复于:2008-06-27 12:26:30
学习学习
#37楼 得分:0回复于:2008-06-27 12:26:52
想学编程,但看不懂,呵呵
#38楼 得分:0回复于:2008-06-27 12:29:09
这论坛不错,内容很多,又详细,呵呵
#39楼 得分:0回复于:2008-06-27 12:52:14
  mark
#40楼 得分:0回复于:2008-06-27 14:32:46
學習下謝謝了
  • cdyzxxj用户头像
  • cdyzxxj
  • (天天向上)
  • 等 级:
#41楼 得分:0回复于:2008-06-27 15:45:14
学学,谢谢
  • qhuwn用户头像
  • qhuwn
  • (天马行空)
  • 等 级:
#42楼 得分:0回复于:2008-06-27 16:23:16
3Q
  • chenjunsheep用户头像
  • chenjunsheep
  • (据说站在火星上看地球,唯一能看)
  • 等 级:
#44楼 得分:0回复于:2008-06-27 21:14:33
引用 7 楼 roy_88 的回复:
引用 6 楼 rockyvan 的回复:
呵呵!楼主有整理知识的好习惯,值得学习!
其实搞过相关内容,没整理过。
以后也多整理下自己的知识,向楼主致敬!学习。



沒有什麼好習慣,都是端午節花了一天時間整理了幾篇..沒貼出來

汗。。。整理了一天,我绝对没那个心思 - -!!
#45楼 得分:0回复于:2008-06-27 22:47:19
真是辛苦楼主了呀
#46楼 得分:0回复于:2008-06-28 01:11:44
mark
#47楼 得分:0回复于:2008-06-28 08:23:06
谢谢楼主
#48楼 得分:0回复于:2008-06-28 13:02:24
真是辛苦楼主了呀
  • basaka1982用户头像
  • basaka1982
  • (被菜鸟啃的鸟菜!!!!)
  • 等 级:
#49楼 得分:0回复于:2008-06-28 14:28:28
做个记号
#50楼 得分:0回复于:2008-06-28 14:35:29
http://www.zzGps.cn 在线技术视频

http://bbs.ZzGps.Cn 技术视频下载

技术视频提供,分类清晰,技术覆盖面广,几千视频助您轻松学习
相关问题
将txt导入到mssql如何处理重复数据?
小小挑战CSDN
SQL过滤重复值(含子查询)
sql怎样过滤重复记录
请教:如何查找、删除表(海量数据)中重复的记录? Oracle / 基础和管理 ...
SQL怎么删除重复行!!!!急救!!!!