sqlserver删除重复数据保留最⼤id的SQL语句
SqlServer 删除重复数据保留最⼤id的SQL语句
多种⽅法:
1--1、⽤于查询重复处理记录(如果列没有⼤⼩关系时2000⽤⽣成⾃增列和临时表处理,SQL2005⽤row_number函数处理)
2
3--II、Name相同ID最⼤的记录,与min相反:
4⽅法1:
5Select*from #T a where not exists(select1from #T where Name=a.Name and ID>a.ID)
6
7⽅法2:
8select 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
9
10⽅法3:
11select*from #T a where ID=(select max(ID) from #T where Name=a.Name) order by ID
12
13⽅法4:
14select 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
15
16⽅法5:
17select*from #T a group by ID,Name,Memo having ID=(select max(ID)from #T where Name=a.Name)
18
19⽅法6:
20select*from #T a where (select count(1) from #T where Name=a.Name and ID>a.ID)=0
21
22⽅法7:
23select*from #T a where ID=(select top1 ID from #T where Name=a.name order by ID desc)
24
25⽅法8:
26select*from #T a where ID!<all(select ID from #T where Name=a.Name)
27
28⽅法9(注:ID为唯⼀时可⽤):
29select*from #T a where ID in(select max(ID) from #T group by Name)
30
31--SQL2005:
32
33⽅法10:
34select ID,Name,Memo from (select*,max(ID)over(partition by Name) as MinID from #T a)T where ID=MinID
35
36⽅法11:
37select ID,Name,Memo from (select*,row_number()over(partition by Name order by ID desc) as MinID from #T a)T where MinID=1
38
39⽣成结果2:
40/*
41ID Name Memo
42----------- ---- ----
433 A A3
445 B B2
45
46(2 ⾏受影响)
47*/
48
49
50--2、删除重复记录有⼤⼩关系时,保留⼤的其中⼀个记录
51--II、Name相同ID保留最⼤的⼀条记录:
52
53⽅法1:
54delete a from #T a where exists(select1from #T where Name=a.Name and ID>a.ID)
55
56⽅法2:
57delete 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
58
59⽅法3:
60delete a from #T a where ID not in (select max(ID) from #T where Name=a.Name)
61
62⽅法4(注:ID为唯⼀时可⽤):
63delete a from #T a where ID not in(select max(ID)from #T group by Name)
64
65⽅法5:
66delete a from #T a where (select count(1) from #T where Name=a.Name and ID>a.ID)>0
67
68⽅法6:
69delete a from #T a where ID<>(select top1 ID from #T where Name=a.name order by ID desc)
71⽅法7:
72delete a from #T a where ID<any(select ID from #T where Name=a.Name)
73
74
75select*from #T
76/*
77ID Name Memo
78----------- ---- ----
793 A A3
805 B B2
81
82(2 ⾏受影响)
83*/
-
-处理表重复记录(查询和删除)
/******************************************************************************************************************************************************
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<a.ID)
⽅法2:
select a.* from #T a join (select min(ID)ID,Name from #T group by Name) b on a.Name=b.Name and a.ID=b.ID
⽅法3:
select * from #T a where ID=(select min(ID) from #T where Name=a.Name)
⽅法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 min(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)
⽅法8:
select * from #T a where ID!>all(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 ⾏受影响)
*/
--2、删除重复记录有⼤⼩关系时,保留其中最⼩的⼀个记录
--> --> (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 ID<a.ID)
⽅法2:
delete a from #T a left join (select min(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 min(ID) from #T where Name=a.Name)
⽅法4(注:ID为唯⼀时可⽤):
delete a from #T a where ID not in(select min(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)
⽅法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 ⾏受影响)
*/
-
-3、删除重复记录没有⼤⼩关系时,处理重复值
--> --> (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 ⾏受影响)
*/
---------------------完整代码(折叠)----------------------
1--处理表重复记录(查询和删除)
2/****************************************************************************************************************************************************** 31、Num、Name相同的重复值记录,没有⼤⼩关系只保留⼀条
42、Name相同,ID有⼤⼩关系时,保留⼤或⼩其中⼀个记录
5整理⼈:中国风(Roy)
6
7⽇期:2008.06.06
8******************************************************************************************************************************************************/ 9
10--1、⽤于查询重复处理记录(如果列没有⼤⼩关系时2000⽤⽣成⾃增列和临时表处理,SQL2005⽤row_number函数处理)
11
12--> --> (Roy)⽣成測試數據
13
14if not object_id('Tempdb..#T') is null
15drop table #T
16Go
17Create table #T([ID]int,[Name]nvarchar(1),[Memo]nvarchar(2))
18Insert #T
19select1,N'A',N'A1'union all
20select2,N'A',N'A2'union all
21select3,N'A',N'A3'union all
22select4,N'B',N'B1'union all
23select5,N'B',N'B2'
24Go
25
26
27--I、Name相同ID最⼩的记录(推荐⽤1,2,3),⽅法3在SQl05时,效率⾼于1、2
28⽅法1:
29Select*from #T a where not exists(select1from #T where Name=a.Name and ID<a.ID)
30
31⽅法2:
32select a.*from #T a join (select min(ID)ID,Name from #T group by Name) b on a.Name=b.Name and a.ID=b.ID
33
34⽅法3:
35select*from #T a where ID=(select min(ID) from #T where Name=a.Name)
36
37⽅法4:
38select 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
39
40⽅法5:
41select*from #T a group by ID,Name,Memo having ID=(select min(ID)from #T where Name=a.Name)
42
43⽅法6:
44select*from #T a where (select count(1) from #T where Name=a.Name and ID<a.ID)=0
45
46⽅法7:
47select*from #T a where ID=(select top1 ID from #T where Name=a.name order by ID)
48
49⽅法8:
50select*from #T a where ID!>all(select ID from #T where Name=a.Name)
51
52⽅法9(注:ID为唯⼀时可⽤):
53select*from #T a where ID in(select min(ID) from #T group by Name)
54
55--SQL2005:
56
57⽅法10:
58select ID,Name,Memo from (select*,min(ID)over(partition by Name) as MinID from #T a)T where ID=MinID
59
60⽅法11:
61
62select ID,Name,Memo from (select*,row_number()over(partition by Name order by ID) as MinID from #T a)T where MinID=1 63
64⽣成结果:
65/*
66ID Name Memo
67----------- ---- ----
681 A A1
694 B B1
70
71(2 ⾏受影响)
72*/
73
74
75--II、Name相同ID最⼤的记录,与min相反:
76⽅法1:
delete in77Select*from #T a where not exists(select1from #T where Name=a.Name and ID>a.ID)
78
79⽅法2:
80select 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 81
82⽅法3:
83select*from #T a where ID=(select max(ID) from #T where Name=a.Name) order by ID
84
85⽅法4:
86select 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
87
88⽅法5:
89select*from #T a group by ID,Name,Memo having ID=(select max(ID)from #T where Name=a.Name)
90
91⽅法6:
92select*from #T a where (select count(1) from #T where Name=a.Name and ID>a.ID)=0
93
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论