如何将以下CTE转换为递归子查询?它是牛顿方法的一种实现。
理由:
1)我有没有权限在DB中创建函数或存储过程
2)我必须在TSQL中做所有事情
3)不使用Oracle
TESTDATA表
PMT t V
6918.26 6 410000
3636.51 14 460000
3077.98 22 630000
1645.14 18 340000
8591.67 13 850000
期望输出
PMT t V Newton
6918.26 6 410000 0.066340421
3636.51 14 460000 0.042449138
3077.98 22 630000 0.024132674
1645.14 18 340000 0.004921588
8591.67 13 850000 0.075982984
_
DECLARE @PMT AS FLOAT
DECLARE @t AS FLOAT
DECLARE @V AS FLOAT
--These will be only for 1 example.
SET @PMT = 6918.26740930922
SET @t = 6
SET @V = 410000
;With Newton (n, i,Fi,dFi) AS (
--base
SELECT
1,
CAST(0.1 AS FLOAT)
,@PMT * (1 - POWER((1 + CAST(0.1 AS FLOAT) / 12), (-@t * 12))) - @V * CAST(0.1 AS FLOAT) / 12
,@PMT * @t * 12 * POWER((1 + CAST(0.1 AS FLOAT) / 12), (-@t * 12 - 1)) - @V
UNION ALL
--recursion
SELECT
n + 1
,i - Fi/dFi
,@PMT * (1 - POWER((1 + i / 12), (-@t * 12))) - @V * i / 12
,@PMT * @t * 12 * POWER((1 + i / 12), (-@t * 12 - 1)) - @V
FROM Newton WHERE n < 500)
--to get the desired value for params above
SELECT [x].i
FROM (
SELECT n, i, Fi, dFi
FROM Newton
WHERE n = 500
) [x]
OPTION (MAXRECURSION 500)
_
我希望Newton将TestData的每条记录评估为独立列。
有什么想法?