遞迴計算冪次總和
在這個練習中,你會用遞迴方式計算冪次總和。這個數列的定義如下:
- 當
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;