开发者

SQL looping and Incrementing

I'm using SQL server 2008

I have the following SQL getting all dates betw开发者_运维问答een today and December 31st 2012. What I now what to do is start with a value of 10000 and increment this value equally so on December 31st I have a value of 150000.

Declare @projection table(d datetime, t decimal)
Declare @d datetime
Declare @t decimal(18, 2)

set @d='20110527'
set @t=100000

While @d<='20121231'
Begin
Insert into @projection values (@d, @t)
set @d=@d+1
End
Select d as DateCol, t as TotalRevenue from @projection

Any help greatly appreciated


This linearly interpolates t from 10000 to 150000.

Declare @projection table(d datetime, t decimal)
Declare @d datetime, @d1 datetime, @d2 datetime
Declare @t decimal(18,4), @t1 decimal(18, 4), @t2 decimal(18,4), @tincr decimal(18,4)

set @d1='20110527'
set @d2='20121231'
set @t1=10000
set @t2=150000

Set @tincr = (@t2-@t1)/DATEDIFF(D, @d1, @d2)

Set @d = @d1
set @t = @t1

While @d<=@d2
Begin
Insert into @projection values (@d, @t)
set @d=@d+1
Set @t=@t+@tincr
End
Select d as DateCol, t as TotalRevenue from @projection

Note that I cranked up the decimal precision to 4 to make rounding errors insignificant.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜