Logo

SQL Server MovingPRODUCT Function

Updated 2023-11-13 21:36:02.603000

Description

Use the scalar function MovingPRODUCT to calculate the product of column values in an ordered resultant table, without the need for a self-join. The product is calculated for each value from the first value in the window to the last value in the window. If the column values are presented to the functions out of order, an error message will be generated.

Syntax

SELECT [westclintech].[wct].[MovingPRODUCT](
  <@Val, float,>
 ,<@Offset, int,>
 ,<@RowNum, int,>
 ,<@Id, tinyint,>
 ,<@Exact, bit,>)

Arguments

@Val

the value passed into the function. @Val is an expression of type float or of a type that can be implicitly converted to float.

@Offset

specifies the window size. The window size (or the number of rows included in the result) is the current row plus the @Offset. @Offset is an expression of type int or of a type that can be implicitly converted to int.

@RowNum

the number of the row within the group for which the sum is being calculated. If @RowNum for the current row in a set is less than or equal to the previous @RowNum and @RowNum is not equal to 1, an error message will be generated. @RowNum is an expression of type int or of a type that can be implicitly converted to int.

@Id

a unique identifier for the MovingPRODUCT calculation. @Id allows you to specify multiple moving sums within a resultant table. @Id is an expression of type tinyint or of a type that can be implicitly converted to tinyint.

@Exact

a bit value which tells the function whether or not to return a NULL value if the number of rows in the window is smaller the @Offset value. If @Exact is 'True' and the number of rows in the window is less the @Offset then a NULL is returned. @Exact is an expression of type bit or of a type that can be implicitly converted to bit.

Return Type

float

Remarks

If @Id is NULL then @Id = 0.

To calculate the running product from the beginning of a dataset or partition, use the RunningPRODUCT function.

If @RowNum is equal to 1, MovingPRODUCT is equal to @Val

@RowNum must be in ascending order.

There may be cases where the order in which the data are returned to the function and the order in which the results are returned are different, generally due to parallelism. You can use OPTION(MAXDOP 1) or OPTION(MAXDOP 1,FORCE ORDER) to help eliminate this problem.

Examples

In this example we simply calculate the product of the current row and the previous 4 rows.

SELECT rn,
       x,
       wct.MovingPRODUCT(   x,      --@Val
                            4,      --@Offset
                            rn,     --@RowNum
                            NULL,   --@Id
                            'False' --@Exact
                        ) as [Product]
FROM
(
    VALUES
        (1, 1),
        (2, 0.5),
        (3, 0.333333333333333),
        (4, 0.25),
        (5, 0.2),
        (6, 0.166666666666667),
        (7, 0.142857142857143),
        (8, 0.125),
        (9, 0.111111111111111),
        (10, 0.1),
        (11, 0.0909090909090909),
        (12, 0.0833333333333333),
        (13, 0.0769230769230769),
        (14, 0.0714285714285714),
        (15, 0.0666666666666667),
        (16, 0.0625),
        (17, 0.0588235294117647),
        (18, 0.0555555555555556),
        (19, 0.0526315789473684),
        (20, 0.05)
) n (rn, x);

This produces the following result.

rnxProduct
11.00000000000000001
20.50000000000000000.5
30.33333333333333300.166666666666666
40.25000000000000000.0416666666666666
50.20000000000000000.00833333333333332
60.16666666666666700.00138888888888889
70.14285714285714300.000396825396825398
80.12500000000000000.000148809523809524
90.11111111111111106.61375661375663E-05
100.10000000000000003.30687830687831E-05
110.09090909090909091.8037518037518E-05
120.08333333333333331.05218855218855E-05
130.07692307692307696.47500647500646E-06
140.07142857142857144.16250416250416E-06
150.06666666666666672.77500277500277E-06
160.06250000000000001.90781440781441E-06
170.05882352941176471.34669252316311E-06
180.05555555555555569.72611266728914E-07
190.05263157894736847.166609333792E-07
200.05000000000000005.374957000344E-07

Here's the result when we change @Exact to 'True' .

rnxProduct
11.0000000000000000NULL
20.5000000000000000NULL
30.3333333333333330NULL
40.2500000000000000NULL
50.20000000000000000.00833333333333332
60.16666666666666700.00138888888888889
70.14285714285714300.000396825396825398
80.12500000000000000.000148809523809524
90.11111111111111106.61375661375663E-05
100.10000000000000003.30687830687831E-05
110.09090909090909091.8037518037518E-05
120.08333333333333331.05218855218855E-05
130.07692307692307696.47500647500646E-06
140.07142857142857144.16250416250416E-06
150.06666666666666672.77500277500277E-06
160.06250000000000001.90781440781441E-06
170.05882352941176471.34669252316311E-06
180.05555555555555569.72611266728914E-07
190.05263157894736847.166609333792E-07
200.05000000000000005.374957000344E-07

You can combine the MovingPRODUCT function with other arithmetic operations. In this example, we calculate the 12-month moving geometric return for a portfolio by adding 1 to the monthly return figures and then subtracting 1 from the result.

SELECT *,
       wct.MovingPRODUCT(1 + r, 11, ROW_NUMBER() OVER (ORDER BY dt), NULL, 'True')
                 - 1 as [12-month Return]
FROM
(
    VALUES
        ('2012-01-31', -0.000225),
        ('2012-02-29', -0.001225),
        ('2012-03-31', 0.00305),
        ('2012-04-30', -0.002175),
        ('2012-05-31', -0.001325),
        ('2012-06-30', -0.0038),
        ('2012-07-31', -0.00258333),
        ('2012-08-31', 0.00189167),
        ('2012-09-30', -0.002175),
        ('2012-10-31', -0.003825),
        ('2012-11-30', -0.00046667),
        ('2012-12-31', 0.00160833),
        ('2013-01-31', -0.000625),
        ('2013-02-28', 0.00135),
        ('2013-03-31', -0.00276667),
        ('2013-04-30', -0.002875),
        ('2013-05-31', 0.00306667),
        ('2013-06-30', 0.00279167),
        ('2013-07-31', 0.001),
        ('2013-08-31', 0.0019),
        ('2013-09-30', -0.001975),
        ('2013-10-31', 0.002525),
        ('2013-11-30', 0.00281667),
        ('2013-12-31', 0.00128333)
) n (dt, r);

This produces the following result.

dtr12-month Return
2012-01-31-0.00022500NULL
2012-02-29-0.00122500NULL
2012-03-310.00305000NULL
2012-04-30-0.00217500NULL
2012-05-31-0.00132500NULL
2012-06-30-0.00380000NULL
2012-07-31-0.00258333NULL
2012-08-310.00189167NULL
2012-09-30-0.00217500NULL
2012-10-31-0.00382500NULL
2012-11-30-0.00046667NULL
2012-12-310.00160833-0.0112187322787753
2013-01-31-0.00062500-0.0116143337962052
2013-02-280.00135000-0.00906611914277999
2013-03-31-0.00276667-0.0148125279726148
2013-04-30-0.00287500-0.0155036624204579
2013-05-310.00306667-0.0111743430414227
2013-06-300.00279167-0.00463146769690947
2013-07-310.00100000-0.00105549585872322
2013-08-310.00190000-0.00104719036226231
2013-09-30-0.00197500-0.000846964308668041
2013-10-310.002525000.00552201882847148
2013-11-300.002816670.00882503091042008
2013-12-310.001283330.00849768924878869

In this example, we combine the XLeratorDB LAG function with the MovingPRODUCT function to calculate the 12-month moving return using the portfolio values.

SELECT *,
       wct.MovingPRODUCT(
                            port / wct.LAG(port, 1, NULL, ROW_NUMBER() OVER (ORDER 
                                      BY DT), NULL),
                            11,
                            ROW_NUMBER() OVER (ORDER BY DT),
                            NULL,
                            'TRUE'
                        ) - 1 as [12-month Return]
FROM
(
    VALUES
        ('2011-12-31', 100000),
        ('2012-01-31', 99666.67),
        ('2012-02-29', 99749.73),
        ('2012-03-31', 100165.35),
        ('2012-04-30', 100165.35),
        ('2012-05-31', 100248.82),
        ('2012-06-30', 99831.12),
        ('2012-07-31', 99997.51),
        ('2012-08-31', 99830.85),
        ('2012-09-30', 99747.66),
        ('2012-10-31', 99997.03),
        ('2012-11-30', 99913.7),
        ('2012-12-31', 100330.01),
        ('2013-01-31', 99911.97),
        ('2013-02-28', 100245.01),
        ('2013-03-31', 99827.32),
        ('2013-04-30', 100076.89),
        ('2013-05-31', 99993.49),
        ('2013-06-30', 99660.18),
        ('2013-07-31', 99327.98),
        ('2013-08-31', 99327.98),
        ('2013-09-30', 99576.3),
        ('2013-10-31', 99908.22),
        ('2013-11-30', 100324.5),
        ('2013-12-31', 100073.69)
) n (dt, port);

This produces the following result.

dtport12-month Return
2011-12-31100000.00NULL
2012-01-3199666.67NULL
2012-02-2999749.73NULL
2012-03-31100165.35NULL
2012-04-30100165.35NULL
2012-05-31100248.82NULL
2012-06-3099831.12NULL
2012-07-3199997.51NULL
2012-08-3199830.85NULL
2012-09-3099747.66NULL
2012-10-3199997.03NULL
2012-11-3099913.70NULL
2012-12-31100330.010.00330009999999969
2013-01-3199911.970.00246120393106319
2013-02-28100245.010.00496522647229192
2013-03-3199827.32-0.00337471990064453
2013-04-30100076.89-0.000883139728459326
2013-05-3199993.49-0.00254696264754073
2013-06-3099660.18-0.00171229171825416
2013-07-3199327.98-0.00669546671712151
2013-08-3199327.98-0.00503722045840571
2013-09-3099576.30-0.00171793503727347
2013-10-3199908.22-0.000888126377353693
2013-11-30100324.500.00411154826615334
2013-12-31100073.69-0.00255476900680085

In this example we use the LAG and the MovingPRODUCT functions to calculate portfolio returns and compare them to the returns on a benchmark. Note that the @Id parameter must be unique for each invocation of the LAG function and for each invocation of the MovingPRODUCT function but that the same @Id parameter can be used in each function once.

SELECT dt,
       port / wct.LAG(   port,                            --@val
                         1,                               --@Offset
                         NULL,                            --@DefaultValue
                         ROW_NUMBER() OVER (ORDER BY dt), --@RowNum
                         0                                --@id
                     ) - 1 as [Monthly Portfolio],
       wct.MovingPRODUCT(   port / wct.LAG(port,                            
                 --@val
                                           1,                               
                                                     --@Offset
                                           NULL,                            
                                                     --@DefaultValue
                                           ROW_NUMBER() OVER (ORDER BY DT), 
                                                     --@RowNum
                                           1                                
                                                     --@Id
                                       ),
                            11,                              --@Offset
                            ROW_NUMBER() OVER (ORDER BY DT), --@RowNum
                            0,                               --@Id
                            'TRUE'                           --@Exact
                        ) - 1 as [12-month Portfolio Return],
       bmk / wct.LAG(   bmk,                             --@val
                        1,                               --@Offset
                        NULL,                            --@DefaultValue
                        ROW_NUMBER() OVER (ORDER BY dt), --@RowNum
                        2                                --@id
                    ) - 1 as [Monthly Benchmark],
       wct.MovingPRODUCT(   bmk / wct.LAG(bmk,                             
                 --@val
                                          1,                               
                                                    --@Offset
                                          NULL,                            
                                                    --@DefaultValue
                                          ROW_NUMBER() OVER (ORDER BY DT), 
                                                    --@RowNum
                                          3                                
                                                    --@Id
                                      ),
                            11,                              --@offset
                            ROW_NUMBER() OVER (ORDER BY DT), --@RowNum
                            1,                               --@Id
                            'TRUE'                           --@Exact
                        ) - 1 as [12-month Benchmark Return]
FROM
(
    VALUES
        ('2011-12-31', 100000, 1257.60),
        ('2012-01-31', 99666.67, 1312.41),
        ('2012-02-29', 99749.73, 1365.68),
        ('2012-03-31', 100165.35, 1408.47),
        ('2012-04-30', 100165.35, 1397.91),
        ('2012-05-31', 100248.82, 1310.33),
        ('2012-06-30', 99831.12, 1362.16),
        ('2012-07-31', 99997.51, 1379.32),
        ('2012-08-31', 99830.85, 1406.58),
        ('2012-09-30', 99747.66, 1440.67),
        ('2012-10-31', 99997.03, 1412.16),
        ('2012-11-30', 99913.7, 1416.18),
        ('2012-12-31', 100330.01, 1426.19),
        ('2013-01-31', 99911.97, 1498.11),
        ('2013-02-28', 100245.01, 1514.68),
        ('2013-03-31', 99827.32, 1569.19),
        ('2013-04-30', 100076.89, 1597.57),
        ('2013-05-31', 99993.49, 1630.74),
        ('2013-06-30', 99660.18, 1606.28),
        ('2013-07-31', 99327.98, 1685.73),
        ('2013-08-31', 99327.98, 1632.97),
        ('2013-09-30', 99576.3, 1681.55),
        ('2013-10-31', 99908.22, 1756.54),
        ('2013-11-30', 100324.5, 1805.81),
        ('2013-12-31', 100073.69, 1810.65)
) n (dt, port, bmk);

This produces the following result.

dtMonthly Portfolio12-month Portfolio ReturnMonthly Benchmark12-month Benchmark Return
2011-12-31NULLNULLNULLNULL
2012-01-31-0.00333329999999998NULL0.0435830152671757NULL
2012-02-290.000833377898549204NULL0.0405894499432342NULL
2012-03-310.00416662781944388NULL0.0313323765450177NULL
2012-04-300NULL-0.00749749728428717NULL
2012-05-310.000833322101904566NULL-0.0626506713593866NULL
2012-06-30-0.00416663258480265NULL0.0395549212793724NULL
2012-07-310.00166671474786617NULL0.0125976390438713NULL
2012-08-31-0.00166664149937323NULL0.0197633616564683NULL
2012-09-30-0.000833309543092131NULL0.0242360903752366NULL
2012-10-310.00250000852150301NULL-0.0197894035414078NULL
2012-11-30-0.000833324749745068NULL0.00284670292318157NULL
2012-12-310.00416669585852580.003300099999999690.007068310525498060.134056933842239
2013-01-31-0.004166649639524510.002461203931063190.05042806358199110.141495416828582
2013-02-280.003333334334214340.004965226472291920.01106060302648020.109103157401441
2013-03-31-0.00416669118991542-0.003374719900644530.03598779940317430.114109636697977
2013-04-300.00250001702940628-0.0008831397284593260.0180857639928880.142827506777976
2013-05-31-0.000833359230087982-0.002546962647540730.02076278347740650.244526188059497
2013-06-30-0.0033333169989368-0.00171229171825416-0.01499932545960740.179215363833911
2013-07-31-0.00333332731287461-0.006695466717121510.04946211121348720.222145694980136
2013-08-310-0.00503722045840571-0.03129801332360460.160950674686119
2013-09-300.00250000050338284-0.001717935037273470.02974947488318840.167199983341085
2013-10-310.00333332329078306-0.0008881263773536930.04459575986441090.243867550419217
2013-11-300.004166624127624360.004111548266153340.02804946087194150.275127455549437
2013-12-31-0.00249998754043124-0.002554769006800850.002680237677274990.269571375482931