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

2个查询结果不一样,哪位分析下,哈哈

szy_temp里面有重复的数据

insert into szy_max    
select gdsid,kqbh,gys,max(lastmodified) 
from szy_temp 
group by gdsid,kqbh,gys 



查询1:

delete szy_temp
from szy_temp a,szy_max b 
where a.gdsid = b.gdsid and a.kqbh = b.kqbh and a.gys = b.gys and a.lastmodified < b.lastmodified 

select * from szy_temp


查询2:

  select *   
  from szy_temp a,szy_max b 
  where a.gdsid = b.gdsid and a.kqbh = b.kqbh and a.gys = b.gys and a.lastmodified = b.lastmodified 
  

------解决方案--------------------
下面的查询1和2的结果是一样的:


--szy_temp里面有重复的数据

declare @szy_temp table(gdsid int,kqbh int,gys int,lastmodified datetime)
declare @szy_max table(gdsid int,kqbh int,gys int,lastmodified datetime)

insert into @szy_temp
values(1,1,1,'2013-01-01 00:00:01'),
      (1,1,1,'2013-01-01 00:00:05')


insert into @szy_max    
select gdsid,kqbh,gys,max(lastmodified) 
from @szy_temp 
group by gdsid,kqbh,gys 



--查询1:

delete @szy_temp
from @szy_temp a,@szy_max b 
where a.gdsid = b.gdsid and a.kqbh = b.kqbh and a.gys = b.gys and a.lastmodified < b.lastmodified 

select * from @szy_temp


--查询2:

select a.*   
from @szy_temp a,@szy_max b 
where a.gdsid = b.gdsid and a.kqbh = b.kqbh and a.gys = b.gys and a.lastmodified = b.lastmodified 
  

------解决方案--------------------
--楼主还是找一条异常的数据,分析之