0

My first time writing a recursive CTE in SQL to calculate the rolling returns. Formula : 100*(1+Returns) for first value, and then (RollingReturns)*(1+Returns)

The table is as below:

+----+--------+--------+----------------+
| ID |  Date  | Return | RollingReturns |
+----+--------+--------+----------------+
|  1 | 1/1/20 | 0.50%  |          100.5 |
|  1 | 1/2/20 | 1.00%  |         101.51 |
|  1 | 1/3/20 | -0.7%  |         100.74 |
|  1 | 1/4/20 | 0.50%  |         101.25 |
+----+--------+--------+----------------+

My attempt at writing the SQL query:

    WITH rn_cte AS (
        SELECT ROW_NUMBER() OVER (ORDER BY DATE ASC) AS RN, DATE 
        FROM TABLE WHERE ID = 1
        ORDER BY RN 
    )
    rr_cte
    AS
    (
        SELECT RN,P.DATE,RETURNS,RETURNS AS ROLLINGRETURNS
        FROM TABLE P 
        JOIN rn_cte ON rn_cte.DATE = p.DATE 
        WHERE P.ID = 1 AND RN = 1
        UNION ALL
        SELECT RN,pm.DATE,pm.RETURNS,(rr_cte.ROLLINGRETURNS)*(1+pm.RETURNS) AS ROLLINGRETURNS
        FROM TABLE pm WHERE pm.ID = 1
        JOIN rr_cte ON rr_cte.RN = pm.RN+1
        ORDER BY pm.DATE ASC 
    )
    SELECT *
    FROM rr_cte 

It gives me an error, not sure what is wrong in it.

Error                                                                     

    ^found "RR_CTE" (at char 145) expecting `SELECT' or `'(''

Any help is appreciated. Thanks in advance!

4

1 回答 1

0

You want something like this:

WITH rn AS (
      SELECT ROW_NUMBER() OVER (PARTITION BY id ORDER BY DATE ASC) AS RN, t.* 
      FROM TABLE t
      WHERE ID = 1
     ),
     cte AS (
      SELECT rn.rn, rn.id, rn.date, return,
             100 * (1 + rn.return) as rollingreturn
      FROM rn
      UNION ALL
      SELECT rn.rn, rn.id, rn.date, rn.return,
             cte.rollingreturn * (1 + rn.return)
      FROM cte JOIN
           rn
           ON cte.id = rn.id AND rn.rn = cte.rn + 1
     )
SELECT *
FROM cte;
于 2020-09-06T21:31:03.470 回答