我被要求在SQL中执行一些excel,例如优化。我需要遍历一些变量(它将采用离散值,跳过0.05)并为每个变量找到最小化距离的值,该距离是该变量的函数。我不太习惯使用动态变量,所以任何形式的帮助都会受到高度赞赏。
--Loop trough each variable to be optimized
declare @j as integer
set @j = 1
while @j <= 50
begin
declare @i as integer
set @i=0
--Here is where I don't know how to do it, I need the variable to be declared in each loop
--If the next deviation is deviating from the prior, means that the prior iteration reached the best fit. Equations in this case are linear
do while @deviation'+'@i <deviation'+'(@i-1)
declare @deviation'+'@i as float
--Update the table each time with the recalculated target variable
Select a.*, a.product_price * b.product_fee as changing_value into calculator
from product_tables a left join fee_table b
on a.Product_code = b.Product_code
Recalculate function distance to be minimized
set @deviation'+'@i = sum(SQRT(Current_value - changing_value)^2)) from calculator
update fee_table
set product_fee = product_fee +@i*0.05 where id_fee = @j
set @i = i+1
end
--- If the condition is met, means that the (@i-1) value is the best fit
update fee_table
set product_fee = product_fee +(@i-1)*0.05 where id_fee = @j
set @j= j+1
end
我提前为您在代码中发现的不一致而道歉。任何形式的帮助都将受到高度赞赏!!