删除重复飞记录
zsnf	ksdm	yxdh	pcdm	zyxh
46525	1239829	12166	1011	1006
46525	1239829	12166	1011	1005
46525	1239829	12166	1011	1003
46525	1239829	12166	1011	1004
46525	1239829	12188	1011	1038
46525	1239829	12188	1011	1039
46525	1239829	12188	1011	1040
46525	1239829	12188	1011	1041
46525	1239829	12556	1011	1029
46525	1239829	12556	1011	1030
46525	1239829	12556	1011	1031
46525	1239829	12556	1011	1032
46525	1239830	10959	1011	1023
46525	1239830	10959	1011	1022
46525	1239830	10959	1011	1020
46525	1239830	10959	1011	1021
46525	1239830	10959	1011	1024
46525	1239830	11341	1011	1002
46525	1239830	11341	1011	1003
46525	1239830	11341	1011	1004
46525	1239830	11341	1011	1005
46525	1239830	11341	1011	1006
46525	1239830	11963	1011	1012
46525	1239830	11963	1011	1013
46525	1239830	11963	1011	1011
46525	1239830	11963	1011	1014
46525	1239830	11963	1011	1015
46525	1239830	12472	1011	1029
46525	1239830	12472	1011	1030
46525	1239830	12477	1011	1038
46525	1239830	12477	1011	1039
49813	1456561	12188	1011	1003
49813	1456561	12188	1011	1002
49813	1456561	12188	1011	1007
49813	1456561	12188	1011	1004
49813	1456561	12188	1011	1005
49813	1456561	12188	1011	1006
这是一个表,我想删除有些字段相同的记录
就是当ksdm字段和yxdh字段前后的相同时,只保留一条记录。
比如说:前8个记录
zsnf	ksdm	yxdh	pcdm	zyxh
46525	1239829	12166	1011	1006
46525	1239829	12166	1011	1005
46525	1239829	12166	1011	1003
46525	1239829	12166	1011	1004
46525	1239829	12188	1011	1038
46525	1239829	12188	1011	1039
46525	1239829	12188	1011	1040
46525	1239829	12188	1011	1041
我就只想保留:
zsnf	ksdm	yxdh	pcdm	zyxh
46525	1239829	12166	1011	1006
46525	1239829	12188	1011	1038
就可以了(zyxh字段取什么完全不用管他)
问下sql语句怎么去写?
------最佳解决方案--------------------删除重复记录有大小关系时,保留大或小其中一个记录
--> --> (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)