Using SQL Cursor, To find the date > 45 days difference from initial date, then again from 2nd date found, again > 45 and find next date

Viewed 39
Declare @quoteReference NVARCHAR(20)
Declare @policyNo NVARCHAR(20)
Declare @todaysDate DATE
Declare @time int
Declare @ddiff int
Declare @cnt varchar(10)

Declare EmpCursor Cursor for
select quote_reference, Miscellaneous_TodaysDate, diff from #temp1

OPEN EmpCursor

FETCH NEXT FROM EmpCursor INTO @quoteReference ,@todaysDate,@ddiff WHILE(@@FETCH_STATUS = 0)
BEGIN
    
    select @quoteReference = quote_reference
           --@policyno = policy_policyNo,
          -- @time = Miscellaneous_CurrentTime
           from #temp1 where
           Miscellaneous_TodaysDate = @todaysDate and diff > 45
    
    if
           @ddiff < 45
            update #temp1
            set counts = 'N'
            where Miscellaneous_TodaysDate = @todaysDate
    else
            update #temp1
            set counts = 'Y'
            where Miscellaneous_TodaysDate = @todaysDate
   FETCH NEXT FROM EmpCursor INTO @quoteReference, @policyNo, @todaysDate
END
CLOSE EmpCursor
DEALLOCATE EmpCursor

The first time quote_reference entered in the database is 31/05/2021. I need another date where the same record came again in the database after more than 45 days (so in this case its 64 days i.e 03/08/2021) then 03/08/2021 date becomes my 2nd initial date and from that date same rules and conditions, i.e. to find date where the same record entered again after more than 45 days difference.

DATA:

enter image description here

1 Answers
Related