我有以下情况(excel格式):
Date Start: 2016-01-01
Date Finish: 2016-12-31
Max Value: 96
Point Value: 0.0972
所以如果我更改上面的那些参数,记录将会改变。例如下面的结果:
Date Value
-----------------------------
2016-01-01 96 --> This value is taken from MaxValue Parameter
2016-01-02 95.9 --> It requires a formula, which is (1 - PointValue) * Previous Value
2016-01-03 95.8
ff.
2016-12-31 67.3 --> Last Record, Based on Date Finish. The formula is the same
有没有人知道如何在SQL脚本中执行此操作?
谢谢。
答案 0 :(得分:2)
您可以使用CTE解决此问题。
DECLARE @StartDate DATE = '2016-01-01'
,@EndDate DATE = '2016-12-31'
,@MaxValue float = 96
,@PointValue float = 0.0972
;WITH ctetest AS (
SELECT @StartDate AS CDate,@MaxValue AS Value
UNION ALL
SELECT dateadd(day,1,CDate) ,(1-@PointValue) * Value
FROM ctetest
WHERE dateadd(day,1,CDate)<=@EndDate
)
SELECT * FROM ctetest OPTION (MAXRECURSION 0)
注意: - 请检查foluma,并在必要时进行更改。