Scenario:I have a set of agreements that have a date the agreement was started. From those agreements I track activities that occur during 3 month periods starting from the start date. I calculate those periods on the fly to report the number of activities that occurred during the current period and the previous period. The tricky part is that the agreement can be suspended for any number of times for any length of time and when this happens the duration of the periods needs to be extended so the person has the full 3 months to collect their activities and be rewarded. The data I have to work is:[code="sql"]select 555 partyid,777 roleid, 12345 memberagreementid,convert(datetime,'1/22/2016') editablestartdateinto memberagreementselect 1 suspensionid,12345 targetentityid, convert(datetime,'3/12/2016') begintime, convert(datetime,'3/30/2016') endtimeinto suspensionunion allselect 2,12345,'5/10/2016','5/30/2016'union allselect 3,12345,'7/10/2016','7/20/2016'union allselect 4,12345,'9/10/2016','9/30/2016'union allselect 5,12345,'11/10/2016','11/30/2016'[/code] The calculation for the incentive periods is:[code="sql"]declare @today datetime = '9/22/2016';withincentivedates as( select roleid,partyid,memberagreementid,editablestartdate,previncstartdate,previncenddate,dateadd(dd,1,previncenddate) currincstartdate,dateadd(mm,3,previncenddate) currincenddatefrom(select row_number() over (PARTITION BY roleid ORDER BY memberAgreementId desc) RN,roleid,partyid,memberagreementid,case when editablestartdate < '9/4/2007' then '9/4/2007' else editablestartdate end editablestartdate,case when dateadd(dd,-1,dateadd(mm,datediff(mm,case when editablestartdate < '9/4/2007' then '9/4/2007' else editablestartdate end ,@today)/3*3,case when editablestartdate < '9/4/2007' then '9/4/2007' else editablestartdate end )) >= @todaythen dateadd(mm,-3,dateadd(mm,datediff(mm,case when editablestartdate < '9/4/2007' then '9/4/2007' else editablestartdate end ,dateadd(mm,-1,@today))/3*3,case when editablestartdate < '9/4/2007' then '9/4/2007' else editablestartdate end))elsedateadd(mm,-3,dateadd(mm,datediff(mm,case when editablestartdate < '9/4/2007' then '9/4/2007' else editablestartdate end ,@today)/3*3,case when editablestartdate < '9/4/2007' then '9/4/2007' else editablestartdate end )) end previncstartdate,case when dateadd(dd,-1,dateadd(mm,datediff(mm,case when editablestartdate < '9/4/2007' then '9/4/2007' else editablestartdate end ,@today)/3*3,case when editablestartdate < '9/4/2007' then '9/4/2007' else editablestartdate end ) )>= @today then dateadd(dd,-1,dateadd(mm,datediff(mm,case when editablestartdate < '9/4/2007' then '9/4/2007' else editablestartdate end ,dateadd(mm,-1,@today))/3*3,case when editablestartdate < '9/4/2007' then '9/4/2007' else editablestartdate end ))elsedateadd(dd,-1,dateadd(mm,datediff(mm,case when editablestartdate < '9/4/2007' then '9/4/2007' else editablestartdate end ,@today)/3*3,case when editablestartdate < '9/4/2007' then '9/4/2007' else editablestartdate end ) )end previncenddate from memberagreement ) previncedates where rn = 1 )select * from incentivedates[/code]memberagreement.memberagreementid joins suspension.targetentityidThe part about the 9/4/2007 date is that the reward period was reset for for agreements starting before 9/4/2007. the proposed solution is to put the baseline period dates in a temp table and run a cursor or loop over the suspension table to update the dates based on the number of days the suspension. It would irk me to do that but a set based solution has evaded me thus far.Final result setroleid, partyid, memberagreementid, editablestartdate, previncstartdate, previncenddate, currincstartdate, currincenddate777, 555, 12345, 1/22/2016, 5/30/2016, 9/7/2016, 9/8/2016, 1/17/2017If run in a cursorresult of first suspensionroleid, partyid, memberagreementid, editablestartdate, previncstartdate, previncenddate, currincstartdate, currincenddate777, 555, 12345, 1/22/2016, 5/10/2016, 8/8/2016, 8/9/2016, 11/8/2016result of second suspensionroleid, partyid, memberagreementid, editablestartdate, previncstartdate, previncenddate, currincstartdate, currincenddate777, 555, 12345, 1/22/2016, 5/30/2016, 8/28/2016, 8/29/2016, 11/28/2016result of third suspensionroleid, partyid, memberagreementid, editablestartdate, previncstartdate, previncenddate, currincstartdate, currincenddate777, 555, 12345, 1/22/2016, 5/30/2016, 9/7/2016, 9/8/2016, 12/8/2016result of fourth suspensionroleid, partyid, memberagreementid, editablestartdate, previncstartdate, previncenddate, currincstartdate, currincenddate777, 555, 12345, 1/22/2016, 5/30/2016, 9/7/2016, 9/8/2016, 12/28/2016result of fifth suspensionroleid, partyid, memberagreementid, editablestartdate, previncstartdate, previncenddate, currincstartdate, currincenddate777, 555, 12345, 1/22/2016, 5/30/2016, 9/7/2016, 9/8/2016, 1/17/2017
↧