How to query date ranges from another table?

1

I have a table with date ranges and another with sales data. How can I mount a query that brings me the sales totals in the last 3 periods from the datas_periodos ??

Tabela (datas_periodos): id_datas;  data1; data2;

id_datas;  data1;        data2;
192       2014-01-04   2014-02-01
193       2014-02-02   2014-03-01
Tabela (vendas): id_vendas; data_venda; vendedor; valor_vendas; 

id_vendas; data_venda; vendedor; valor_vendas;  
1953       2014-01-02  maria      30000,00
3444       2014-01-02  joao       15000,00
3212       2014-02-03  antonio    34202,00

I would need an output like this:

data1          data2      valor total
2014-01-04   2014-02-01    150000,00
2014-02-02   2014-03-01    300000,00

Thanks for the help.

    
asked by anonymous 13.02.2015 / 03:15

1 answer

1

Nothing complicated:

Select data1, data2, SUM(valor_vendas) as vendas_periodo 
From datas_periodos, vendas
Where data_venda Between data1 and data2
Group by data1

See SQLFiddle     

13.02.2015 / 11:24