2016-04-28 83 views
1

我有以下查询来选择(将用于更新语句)基于min服务日期删除重复项并保留最新的svc日期。删除基于重复的日期

select st.SubID, st.RecordNo, st.Fname, st.Lname, st.MemberID, st.ServiceDate, IsDeduped, DedupCriteria 
     from stagingtable st 
     join (select MemberID 
        from stagingtable 
        where SubID = 99999 
          and waveseqid = 1 
        group by MemberID 
        having count(*) > 1) st2 
     on st.MemberID = st2.MemberID 
     and st.ServiceDate = (Select min(ServiceDate) from stagingtable s where s.subid = 99999 and s.waveseqid = 1 and st.MemberID = s.MemberID) 
where SubID = 99999 
     and waveseqid = 1 
     order by RecordNo 

这似乎在拉仅某个倍数拉与同一日期为MEMBERID:

SurveyID RecordNo Fname Lname MemberID Option9 IsDeduped DedupCriteria 
99999 1 John Doe 123 10/1/2015 0 NULL x These show on the query 
99999 2 John Doe 123 10/1/2015 0 NULL x These show on the query 
99999 3 John Doe 123 10/8/2015 0 NULL But expected these as well 
99999 4 John Doe 123 10/12/2015 0 NULL But expected these as well 
99999 4 John Doe 123 10/14/2015 0 NULL But expected these as well 
99999 6 John Doe 123 10/29/2015 0 NULL But expected these as well 
99999 7 John Doe 123 12/14/2015 0 NULL But expected these as well 

回答

1

你“AND”语句结果限制为与最低服务迄今为止,只有行。

and st.ServiceDate = (Select min(ServiceDate) from stagingtable s where s.subid = 99999 and s.waveseqid = 1 and st.MemberID = s.MemberID) 

这就是为什么你得到两行,而不是全部。