递归计算幂的和
在本练习中,您将用递归方式计算幂的和。该数列定义如下:
- 当
step = 1时,result = 1 - 当
step > 1时,result + step^step
该数列的值增长很快,且不会收敛。本练习的任务是计算当 step = 9 时的幂和。
本练习是课程的一部分
SQL Server 中的分层与递归查询
练习说明
- 定义 CTE
calculate_potencies,包含字段step和result。 - 参考数列的定义,初始化
step和result。 - 在递归部分将下一步添加为
POWER(step + 1..,并将其累加到result。
交互式实操练习
通过完成这段示例代码来试试这个练习。
-- Define the CTE calculate_potencies with the fields step and result
WITH ___ (___, ___) AS (
SELECT
-- Initialize step and result
___,
___
UNION ALL
SELECT
step + 1,
-- Add the POWER calculation to the result
___ + POWER(step + 1, ___ + 1)
FROM calculate_potencies
WHERE step < 9)
SELECT
step,
result
FROM calculate_potencies;