Sql Server 2005ではrow_を使用しています。number()重複記録を一つのクエリーから削除する方法


次はどうやって表の重複記録を削除しますか?
 
If Exists(Select * From tempdb.Information_Schema.Tables Where Table_Name Like '#Temp%')
Drop Table #temp
Create Table #temp ([Id] int, [Name] varchar(50), [Age] int, [Sex] bit default 1)
Go
Insert Into #temp ([Id] , [Name] , [Age] , [Sex] ) Values(1,'James',25,default)
Insert Into #temp ([Id] , [Name] , [Age] , [Sex] ) Values(1,'James',25,default)
Insert Into #temp ([Id] , [Name] , [Age] , [Sex] ) Values(1,'James',25,default)
Insert Into #temp ([Id] , [Name] , [Age] , [Sex] ) Values(2,'Lisa',24,0)
Insert Into #temp ([Id] , [Name] , [Age] , [Sex] ) Values(2,'Lisa',24,0)
Insert Into #temp ([Id] , [Name] , [Age] , [Sex] ) Values(2,'Lisa',24,0)
Insert Into #temp ([Id] , [Name] , [Age] , [Sex] ) Values(3,'Mirsa',23,0)
Insert Into #temp ([Id] , [Name] , [Age] , [Sex] ) Values(3,'Mirsa',23,0)
Insert Into #temp ([Id] , [Name] , [Age] , [Sex] ) Values(3,'Mirsa',23,0)
Insert Into #temp ([Id] , [Name] , [Age] , [Sex] ) Values(4,'John',26,default)
Insert Into #temp ([Id] , [Name] , [Age] , [Sex] ) Values(5,'Abraham',28,default)
Insert Into #temp ([Id] , [Name] , [Age] , [Sex] ) Values(6,'Lincoln',30,default)
Delete T From
(Select Row_Number() Over(Partition By [ID],[Name],[Age],[Sex] order By [ID]) As RowNumber,* From #Temp)T
Where T.RowNumber > 1
Select * From #temp
は最後から二番目のシナリオに注意して、私達は一つのクエリでこの機能を実現します。あなたは自分でT-SQL scriptを実行して効果を見ることができます。あなたの開発に役立ちます。作者:Petter Liu