2016-11-20 14 views
1

SQLサーバーを使用して、f(x)= 3x & 3 -2X^2 + 15の関数値を計算して出力するwhileループを作成します。< = 25 (1,3,5、... 23,25)。私は理論的に、私は45640 @x = 25SQL Whileループで関数を解く

を取得する必要があります

My Results

を取得

この

は、私がこれまで

My Query

書き出されたものであり、これは結果であり、

ここで何が間違っていますか?

答えて

1

は、次の

Declare @SUMSQUARE Integer, @x Integer 
SET @X = 1 

WHILE (@X <= 25) 
BEGIN 

SELECT @SUMSQUARE = (3*power(@X,3))-(2*power(@X,2))+15 
PRINT @SUMSQUARE 
SET @X = @X + 2 

END 
よう power機能を使用する必要があります
1

whileループの代わりに再帰的なcteを使用して、すべての奇数番号< = 25を生成し、それらの番号に関数を適用できます。

with oddnums_cte as (select 1 num 
        union all 
        select num+2 from oddnums_cte where num < 25) 
select num, (3*power(num,3))-(2*power(num,2))+15 as function_value 
from oddnums_cte 
関連する問題