我有以下t-sql:
declare @startno int = 1, @finishno int = 365, @AfterO float = 97
declare @daysbet int = 80, @decVal float = 0.10 / 100;
;with ctetest as
(
select @startno as oDay, 1 as oBet, @AfterO as Prod
union all
select oDay +1, case when ((oBet + 1) = @daysbet) then 0 else (oBet + 1) end,
case when (Prod = 0) then @AfterO else
case when oBet = 0 then 0 else (Prod - (@decVal * Prod)) end
end
from ctetest
where oDay+1 <= @finishno
)
select * from ctetest option (maxrecursion 0)
结果是:
----------------------------
oDay oBet Prod
----------------------------
1 1 97
2 2 96.903
3 3 96.806
ff.----->
80 0 89.628
81 1 0
82 2 97
ff.----->
364 44 93.008
365 45 92.915
但我想要的结果不是那样的,结果应该是这样的:
oDay oBet Prod
----------------------------
1 1 97
2 2 96.903
3 3 96.806
ff.----->
80 0 0
81 1 97
82 2 96.903
ff.----->
364 44 92.9
365 45 92.8
从上面的公式中,似乎case when oBet = 0
读取了oBet
列中的上一条记录。我想阅读Prod
列中oBet
列旁边的值。
请指教。谢谢。
答案 0 :(得分:3)
更改
case when oBet = 0 then 0 else (Prod - (@decVal * Prod)) end
到
case when ((oBet + 1) = @daysbet) then 0 else (Prod - (@decVal * Prod)) end
因为oBet在下一次迭代中实际上变为0(在oDay = 81)