Run the following code snippet and check the output. It is as desired!
Create table #Projection
(
SequenceNum int identity(1,1),
DateOfAppearance datetime,
Offset int --- this is offset to calculate next projected date
)
INSERT INTO #Projection(DateOfAppearance,Offset)values('25 July 2013',4)
INSERT INTO #Projection(DateOfAppearance,Offset)values(NULL,4)
INSERT INTO #Projection(DateOfAppearance,Offset)values(NULL,4)
INSERT INTO #Projection(DateOfAppearance,Offset)values(NULL,4)
INSERT INTO #Projection(DateOfAppearance,Offset)values(NULL,4)
--select * from #Projection
declare @maxSequenceNum int
declare @sequenceNumCtr int
declare @previousDate datetime
declare @newDate datetime
declare @currentOffset int
set @maxSequenceNum = (select MAX(SequenceNum) from #Projection)
set @sequenceNumCtr = 1
while @sequenceNumCtr <= @maxSequenceNum
begin
if @sequenceNumCtr = 1
begin
-- fetch date from table only if it is first record
set @previousDate = (select DateOfAppearance from #Projection where SequenceNum = 1)
end
else
begin
set @currentOffset = (select Offset from #Projection where SequenceNum = @sequenceNumCtr)
set @newDate = DATEADD(day, @currentOffset, @previousDate)
update #Projection set DateOfAppearance = @newDate where SequenceNum = @sequenceNumCtr
set @previousDate = @newDate -- for next record
end
set @sequenceNumCtr += 1
end
select * from #Projection
drop table #Projection
SequenceNum DateOfAppearance Offset
1 2013-07-25 00:00:00.000 4
2 2013-07-29 00:00:00.000 4
3 2013-08-02 00:00:00.000 4
4 2013-08-06 00:00:00.000 4
5 2013-08-10 00:00:00.000 4
Please note that I have used temporary table just for the demo purpose.