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

感觉第3个语句,要了等于没要,没有意义
过程中有如下3个语句。每次执行到第3个语句就卡住了。

不看业务,光从理论上看,感觉第3个语句要了等于没要。



   
insert #temp_gdsid  
select distinct gdsid,gys, rtptype, max(lastmodified) 
from #temp_ok
group by gdsid,gys,rtptype
--#temp_gdsid 中的每一组gdsid,gys,rtptype 在 #temp_ok 中都应该存在



insert #temp_out 
select distinct a.*
  from #temp_ok a, #temp_gdsid  b  
 where a.gdsid = b.gdsid and a.gys = b.gys and a.rtptype  =b.fmid and a.lastmodified =  b.lastmodified 
 --#temp_out 中的每一组gdsid,gys,rtptype 在 #temp_ok 中都应该存在
--所以下面的语句感觉没必要要了。

insert #temp_out 
select distinct a.*
  from #temp_ok a 
 where not exists(select b.* from #temp_out b where a.gdsid=b.gdsid and a.gys = b.gys and a.rtptype = b.rtptype ) 

------解决方案--------------------
--1.以gdsid,gys, rtptype分组,找到lastmodified最大的lastmodified值的1条(或N条)记录
insert #temp_gdsid
select distinct gdsid,gys, rtptype, max(lastmodified)  --#1.这儿的distinct应该是多余的。
from #temp_ok
group by gdsid,gys,rtptype
--#temp_gdsid 中的每一组gdsid,gys,rtptype 在 #temp_ok 中都应该存在

--2.把1中的具有最大值的1条或N条记录的其它字段也筛选出来,放到#temp_out中,并且放入前过滤重复。
insert #temp_out 
select distinct a.*
from #temp_ok a, #temp_gdsid  b  
where a.gdsid = b.gdsid and a.gys = b.gys and a.rtptype  =b.fmid and a.lastmodified =  b.lastmodified   --#2.楼主这儿写错了吧?a.rtptype  =b.fmid,如果没有错,那么下面的那段就不能省略。
--#temp_out 中的每一组gdsid,gys,rtptype 在 #temp_ok 中都应该存在
--所以下面的语句感觉没必要要了。

--3.正如楼主所说:这儿一条记录也筛选不出来。除非#2说的那样,这条语句才有意义。
insert #temp_out 
select distinct a.*
from #temp_ok a 
where not exists(select b.* from #temp_out b where a.gdsid=b.gdsid and a.gys = b.gys and a.rtptype = b.rtptype )