Query to return recurrence values, which have not yet been created in the database

1

I need to query the database where the records are not yet saved. I'll explain better:

In a system that I am developing, the user can register a recurring expense, that is, that will repeat every month / days (according to the user's choice). With this I create a job that will create an expense (it will create a record in the despesas table) on the configured date and schedule the creation of the next one. This recurring expense may have a predetermined closing date or may be forever.

What I need is to do a query that will add up the expenses per month, for example:

table recorrencia

AndIstillhavethetablewheretheexpensesare:

tabledespesas

Let's say today was 12/1/2015 and I would like to know the amount of my expenses up to 04/30/2015. I need you to return the following:

That is, calculates when a recurring expense will generate an expense, join with the expenses table and do the calculation.

How could I do this query by sql? with a view or something?

I'm using the postgresql database with the Ruby on Rails programming language.

Thank you

    
asked by anonymous 12.12.2015 / 18:05

1 answer

-2
SELECT SUM(value) FROM despesas 
WHERE start_date BETWEEN ("01/01/2016") AND ("31/01/2016")
    
14.12.2015 / 16:55