日期:2014-05-17  浏览次数:20402 次

删除重复飞记录
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语句怎么去写?

------解决方案--------------------
delete a from tb a
where exists(select 1 from tb where ksdm=a.ksdm and yxdh=a.yxdh and zyxh>a.zyxh)
------解决方案--------------------
修改
delete a from tb a
 where exists(select 1 from tb where ksdm=a.ksdm and yxdh=a.yxdh and zyxh<a.zyxh) 
------解决方案--------------------
delete a from tb a
 where not exists(select 1 from tb where ksdm=a.ksdm and yxdh=a.yxdh and zyxh> a.zyxh)
  
------解决方案--------------------
DELETE a
FROM   tb a
WHERE  NOT EXISTS (SELECT 1
                   FROM   tb
                   WHERE  ksdm = a.ksdm
                          AND yxdh = a.yxdh
                          AND zyxh > a.zyxh)

------解决方案--------------------
最好这样:
create table tb(zsnf int,ksdm int,yxdh int,pcdm int,zyxh int)
insert into tb select 46525,1239829,12166,1011,1006