Quantcast
Channel: SQLServerCentral » SQL Server 2014 » Development - SQL Server 2014 » Latest topics
Viewing all articles
Browse latest Browse all 3145

Reverse recursion for some specific statistical calculation

$
0
0
Hi All, First of all, I couldn't come up with a better title explaining my t-sql challenge. Sorry :)I have been trying to wrap my head around this for days. I have a table :id + A + B 1 |0,11| 02 |0,45| 0,53 |0,85| 0,75I need to calculate the following : F = 0,85 * ( 1 - 0,75 * ( 1 - 0,45 * ( 1 - 0,5 * ( 1 - 0,11 * ( 1 - 0 )))))In which F = A3 * ( 1 - B3 * ( 1 - A2 * ( 1 - B2 * ( 1 - A1 * ( 1 - B1 )))))It seemed quite easy at first glance.Is there any other way perhaps. Just a suggestion would do, as I can understand this not a free consultancy forum :).I Built it up via string concatenation and thought to execute the dynamic sql with sp_exec and get the result.As I don't like dynamic sql I was wondering If there is any other way..[code="sql"]ALTER PROC [dbo].[Tools_Serial_FE] AS BEGINDECLARE @IP FLOAT, @IF FLOAT, @FE FLOAT, @id INT, @end INT, @iterator INT, @MathSequence VARCHAR(4000)DECLARE efcursor CURSOR FOR SELECT id,[IP],[IF] FROM TEST.ff ORDER BY id DESC-- id,IP,IF,(FE)OPEN efcursorSET @iterator = 0SET @MathSequence = 'SELECT ROUND( ('SELECT @end = COUNT(id) FROM TEST.ffWHILE @end > @iteratorBEGINFETCH NEXT FROM efcursor INTO @id,@IP,@IFSET @MathSequence = @MathSequence + CAST(@IP AS VARCHAR(255)) + ' * ( 1 - ' + CAST(@IF AS VARCHAR(255)) + ' * ( 1 - ' SET @iterator += 1 ENDSELECT @MathSequence = SUBSTRING(@MathSequence,1, (len(@MathSequence) - 8) ) + REPLICATE(')',(@iterator*2)-1) + ' ) , 2 ) AS FE'PRINT @MathSequencePRINT '...retrieve calculation via sp_exec ...' CLOSE efcursorDEALLOCATE efcursorEND[/code]Any suggestion would be greatly appreciated. Kind Regards, Dennes

Viewing all articles
Browse latest Browse all 3145

Trending Articles