Click here to Skip to main content
15,890,741 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Hello Everyone !
Need a MS Sql query to solve below scenario

SQL
Create table Projection
AnyPrimaryKey int
DateOfAppearance datetime,
Offset int  --- this is offset to calculate next projected date

 INSERT INTO Projection(AnyPrimaryKey,DateOfAppearance,Offset)values(1,25th July 2013,4)
 INSERT INTO Projection(AnyPrimaryKey,DateOfAppearance,Offset)values(2,NULL,4)
 INSERT INTO Projection(AnyPrimaryKey,DateOfAppearance,Offset)values(3,NULL,4)
 INSERT INTO Projection(AnyPrimaryKey,DateOfAppearance,Offset)values(4,NULL,4)
 INSERT INTO Projection(AnyPrimaryKey,DateOfAppearance,Offset)values(5,NULL,4)


Assume this table is prepopulated with 1st row as DateOfAppearance= 25th July 2013 and offset as 4(days). then to calculate next row , formula is DATEADD(day,Offset,DateOfAppearance).so next row becomes 2,29thJulty2013,4. and third row will be 3,2nd August,2013. Each row is contructed using offset from a row before it.

I want a query which does this in SQl for all the columns i.e calculate projected dates using offset and DateOfAppearance column of the row before it. I dont want to use cursors for this.

Thanks in advance
Posted
Comments
[no name] 25-Jul-13 11:32am    
If you know the formula and all that, why can't you do this? What have you tried to do for yourself? What is wrong with the code that you have written?
devcode007 25-Jul-13 13:12pm    
I have used cursor for this first.
Now i have written an update query to do this which is like
UPDATE t
SET t.DateOfAppearance=DATEADD(day,tmp.offset,tmp.DateOfAppearance)
FROM Projection t
INNER JOIN Projection tmp on tmp.SequenceNumber + 1 = t.SequenceNumber
This is making only second row updated properly but not the rest because for the rest of rows, the second row which is updated is not available for calculating 3rd and so forth.

Hope that makes sense now

devcode007 25-Jul-13 13:13pm    
i have added SequenceNumber as SequenceNumber int IDENTITY(1,1).

1 solution

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


CSS
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.
 
Share this answer
 
Comments
devcode007 25-Jul-13 18:50pm    
Thanks Nikhil. I was hoping a solution where no looping is used but guess this is better than using cursor. Thanks again

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900