How to calculate Running Multiplication

Pரதீப்

I have two tables

WAC table

ID  wac_inc             item
--  -----------------   ----
1   2.310000000000000   A
2   1.100000000000000   A
3   2.130000000000000   A
4   1.340000000000000   A

Baseline Table

item    baseline
----    ------------------
A       10.000000000000000

Expected Result

ID  wac_inc             item    Running_Mul   
--  -----------------   ----    -----------
1   2.310000000000000   A       10.231     --  10 * (1+(2.310000000000000/100))
2   1.100000000000000   A       10.343541  --  10.231 * (1+(1.100000000000000/100))
3   2.130000000000000   A       10.563858  --  10.343541 * (1+(2.130000000000000/100))
4   1.340000000000000   A       10.705413  --  10.563858 * (1+(1.340000000000000/100))

Formula to find running_mul is

Baseline * (1 + (wac_inc/100))

SQLFIDDLE

here for every row previous row Running_Mul value is the baseline and for the first row baseline will be coming from baseline table.

Hope i made it clear. AFAIK we can do this using CURSOR but i want to avoid RBAR as much as possible. Can anyone suggest me the better way of doing it.

Giorgi Nakeuri

Try:

DECLARE @t TABLE
    (
      ID INT ,
      wac DECIMAL(30, 10) ,
      item CHAR(1)
    )
DECLARE @b TABLE
    (
      item CHAR(1) ,
      baseline DECIMAL(30, 10)
    )

INSERT  INTO @t
VALUES  ( 1, 2.31, 'A' ),
        ( 2, 1.10, 'A' ),
        ( 3, 2.13, 'A' ),
        ( 4, 1.34, 'A' )


INSERT  INTO @b
VALUES  ( 'A', 10 );


WITH    ordercte
          AS ( SELECT   * ,
                        ROW_NUMBER() OVER ( PARTITION BY item ORDER BY ID ) AS rn
               FROM     @t
             ),
        rec
          AS ( SELECT   t.item ,
                        t.ID ,
                        t.wac ,
                        t.rn ,
                        b.baseline * ( 1 + ( t.wac / 100 ) ) AS m
               FROM     ordercte t
                        JOIN @b b ON b.item = t.item
               WHERE    t.rn = 1
               UNION ALL
               SELECT   t.item ,
                        t.ID ,
                        t.wac ,
                        t.rn ,
                        c.m * ( 1 + ( t.wac / 100 ) )
               FROM     ordercte t
                        JOIN rec c ON t.item = c.item
                                      AND t.rn = c.rn + 1
             )
    SELECT  id ,
            wac ,
            item ,
            m
    FROM    rec

Output:

id  wac             item    m
1   2.3100000000    A       10.231000
2   1.1000000000    A       10.343541
3   2.1300000000    A       10.563858
4   1.3400000000    A       10.705414

EDIT1

I was trying to implement LOG EXP trick but could not manage unless @usr lead me to solution. So all credits to user @usr:

WITH    ordercte
          AS ( SELECT   t.ID ,
                        t.wac ,
                        t.item ,
                        b.baseline ,
                        ROW_NUMBER() OVER ( PARTITION BY t.item ORDER BY ID ) AS rn
               FROM     @t t
                        JOIN @b b ON b.item = t.item
             )
    SELECT  baseline
            * EXP(SUM(LOG(( 1 + ( wac / 100 ) ))) OVER ( PARTITION BY item ORDER BY rn )) AS m
    FROM    ordercte

Or just:

SELECT  t.ID, t.wac, t.item, baseline
        * EXP(SUM(LOG(( 1 + ( wac / 100 ) ))) OVER ( PARTITION BY t.item ORDER BY t.ID )) AS m
FROM    @t t
        JOIN @b b ON b.item = t.item  

if ID is the field you order by.

Output:

ID  wac             item    m
1   2.3100000000    A       10.231
2   1.1000000000    A       10.343541
3   2.1300000000    A       10.5638584233
4   1.3400000000    A       10.7054141261722

EDIT2

For SQL 2008 use:

WITH    cte
          AS ( SELECT   t.ID ,
                        t.wac ,
                        t.item ,
                        baseline ,
                        ( SELECT    SUM(LOG(( 1 + ( wac / 100 ) )))
                          FROM      @t it
                          WHERE     it.item = t.item AND it.ID <= t.ID
                        ) AS e
               FROM     @t t
                        JOIN @b b ON b.item = t.item
             )
    SELECT  ID, wac, item, baseline * EXP(e) AS m
    FROM    cte

EDIT3

Here is complete solution for SQL Server 2008 with dialing with NULLs and negative values:

WITH    cte
          AS ( SELECT   t.ID ,
                        t.wac ,
                        t.item ,
                        b.baseline , 
                        ca.e,
                        ca.n,
                        ca.m
               FROM     @t t
               JOIN @b b ON b.item = t.item
               CROSS APPLY(SELECT   SUM(LOG(ABS(NULLIF( 1 +  wac / 100 , 0)))) as e,
                                    SUM(SIGN(CASE WHEN 1 +  wac / 100 < 0 THEN 1 ELSE 0 END)) AS n,
                                    MIN(ABS(1 +  wac / 100)) AS m
                          FROM      @t it
                          WHERE     it.item = t.item AND it.ID <= t.ID
                          ) ca
             )
    SELECT  ID, wac, item, baseline *
                        CASE
                            WHEN m = 0 THEN 0
                            WHEN n % 2 = 1 THEN -1 * EXP(e)
                            ELSE EXP(e) 
                        END as Result
    FROM    cte

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to find Running Multiplication

From Dev

How to calculate multiplication of each row and show it with jquery

From Dev

How to calculate multiplication and percentage in user-form?

From Dev

How to calculate multiplication of each row and show it with jquery

From Dev

How to calculate multiplication of two numbers recursively

From Dev

How to calculate "running total" in SQL

From Dev

how to calculate running median efficiently

From Dev

How to calculate a running total in view?

From Dev

How to efficiently calculate huge matrix multiplication (tfidf features) in Python?

From Dev

How to efficiently calculate huge matrix multiplication (tfidf features) in Python?

From Dev

How to calculate multiplication of numbers in a html table below specific headings - IONIC

From Java

How to calculate running sum for each group in MySQL

From Dev

How to calculate the running total using aggregate

From Dev

how to calculate the running mode value using modeest

From Dev

how to calculate the turing machine running time?

From Dev

How to calculate running time of an executable file?

From Dev

How to efficiently calculate running maxima in a Pandas dataframe?

From Dev

How to calculate theoretical running time for a created algorithm?

From Dev

How to calculate running time of an executable file?

From Dev

Rails How to calculate Running Total on a View?

From Dev

Oracle Running Multiplication

From Dev

R: Efficiently calculate multiplication'sum

From Dev

Efficient way to calculate array multiplication

From Dev

R: Efficiently calculate multiplication'sum

From Dev

How to calculate view-space position from screen-space position without matrix multiplication

From Dev

Running Multiplication in T-SQL

From Dev

How do I optimize sapply in R to calculate running totals on a dataframe

From Dev

How to calculate running balance of banking table using mysql

From Dev

How to calculate running total (month to date) in SQL Server 2008

Related Related

  1. 1

    How to find Running Multiplication

  2. 2

    How to calculate multiplication of each row and show it with jquery

  3. 3

    How to calculate multiplication and percentage in user-form?

  4. 4

    How to calculate multiplication of each row and show it with jquery

  5. 5

    How to calculate multiplication of two numbers recursively

  6. 6

    How to calculate "running total" in SQL

  7. 7

    how to calculate running median efficiently

  8. 8

    How to calculate a running total in view?

  9. 9

    How to efficiently calculate huge matrix multiplication (tfidf features) in Python?

  10. 10

    How to efficiently calculate huge matrix multiplication (tfidf features) in Python?

  11. 11

    How to calculate multiplication of numbers in a html table below specific headings - IONIC

  12. 12

    How to calculate running sum for each group in MySQL

  13. 13

    How to calculate the running total using aggregate

  14. 14

    how to calculate the running mode value using modeest

  15. 15

    how to calculate the turing machine running time?

  16. 16

    How to calculate running time of an executable file?

  17. 17

    How to efficiently calculate running maxima in a Pandas dataframe?

  18. 18

    How to calculate theoretical running time for a created algorithm?

  19. 19

    How to calculate running time of an executable file?

  20. 20

    Rails How to calculate Running Total on a View?

  21. 21

    Oracle Running Multiplication

  22. 22

    R: Efficiently calculate multiplication'sum

  23. 23

    Efficient way to calculate array multiplication

  24. 24

    R: Efficiently calculate multiplication'sum

  25. 25

    How to calculate view-space position from screen-space position without matrix multiplication

  26. 26

    Running Multiplication in T-SQL

  27. 27

    How do I optimize sapply in R to calculate running totals on a dataframe

  28. 28

    How to calculate running balance of banking table using mysql

  29. 29

    How to calculate running total (month to date) in SQL Server 2008

HotTag

Archive