I have this table, only two columns, each record stores an interest rate for a given month:

```
id     rate
===========
199502 3.63
199503 2.60
199504 4.26
199505 4.25
...    ...
201704 0.79
201705 0.93
201706 0.81
201707 0.80
201708 0.14

```

Based on this rates, I need to create another table of accumulated rates which similar structure, whose data is calculated as function of a YYYYMM (month/year) parameter, this way (this formula is legally mandatory):

1. The month given as parameter has always rate of 0 (zero)
2. The month immediately previous has always a rate of 1 (one)
3. The previous months' rates will be (one) plus the sum of rates of months between that given month and the month given as parameter.

I'll clarify this rules with this example, given parameter `201708`:

```
SOURCE         CALCULATED
id     rate    id       rate
===========    =============
199502 3.63    199502 360.97 (1 + sum(rate(199503) to rate(201707)))
199503 2.60    199503 358.37 (1 + sum(rate(199504) to rate(201707)))
199504 4.26    199504 354.11 (1 + sum(rate(199505) to rate(201707)))
199505 4.25    199505 349.86 (1 + sum(rate(199506) to rate(201707)))
...    ...     ...    ...
201704 0.79    201704   3.54 (1 + rate(201705) + rate(201706) + rate(201707))
201705 0.93    201705   2.61 (1 + rate(201706) + rate(201707))
201706 0.81    201706   1.80 (1 + rate(201707))
201707 0.80    201707   1.00 (per definition)
201708 0.14    201708   0.00 (per definition)

```

Now I've already implemented a VB.NET function that reads the source table and generates the calculated table, but this is done in runtime at each client machine:

```
Public Function AccumRates(targetDate As Date) As DataTable
    Dim dtTarget = Rates.Clone
    Dim targetId = targetDate.ToString("yyyyMM")
    Dim targetIdAnt = targetDate.AddMonths(-1).ToString("yyyyMM")
    For Each dr In Rates.Select("id<=" & targetId & " and id>199412")
        If dr("id") = targetId Then
            dtTarget.Rows.Add(dr("id"), 0)
        ElseIf dr("id") = targetIdAnt Then
            dtTarget.Rows.Add(dr("id"), 1)
        Else
            Dim intermediates =
                Rates.Select("id>" & dr("id") & " and id<" & targetId).Select(
                    Function(ldr) New With {
                        .id = ldr.Field(Of Integer)("id"),
                        .rate = ldr.Field(Of Decimal)("rate")}
                    ).ToArray
            dtTarget.Rows.Add(
                dr("id"),
                1 + intermediates.Sum(
                    Function(i) i.rate))
        End If
    Next
    Return dtTarget
End Function

```

My question is how can I put this as a query in my database so it can be used dynamically by other queries which would use these accumulated rates to update debts to any given date.

Thank you very much! 

**EDIT**

I managed to make a query that returns the data I want, now I just don't know how to encapsulate it so that it can be called from another query passing any `id` as argument (here I did it using a `SET ...` statement):

```
SET @targetId=201708;
SELECT 
    id AS id_acum,
    COALESCE(1 + (SELECT 
                    SUM(taxa)
                FROM
                    tableSelic AS ts
                WHERE
                    id > id_acum AND id < @targetId
                LIMIT 1),
            IF(id >= @targetId, 0, 1)) AS acum
FROM
    tableSelic
    WHERE id>199412;

```

That's because I'm pretty new to MySQL, I'm used to MS-Access where parametrized queries are very straightfoward to create.