If I understand correctly, this should do it:set nocount ondeclare @date datetimeset @date = '6/1/2001'declare @tb table (PolicyNumber int, BenefitAmount money, EffectiveDate datetime)insert @tbselect 123, 20, '1/1/2001' union allselect 123, 30, '6/1/2001' union allselect 123, 40, '1/1/2003' union allselect 321, 20, '1/1/2007'select b.policyNumber ,b.BenefitAmount ,b.EffectiveDatefrom ( select policyNumber ,max(EffectiveDate) EffectiveDate from @tb where EffectiveDate <= @date group by policyNumber ) ajoin @tb b on b.policyNumber = a.PolicyNumber and b.EffectiveDate = a.Effectivedateoutput:policyNumber BenefitAmount EffectiveDate ------------ --------------------- -------------------------123 30.0000 2001-06-01 00:00:00.000
Be One with the OptimizerTG