How to view date ranges from another table?

Asked

Viewed 58 times

1

I have a table with date ranges and another with sales data. How can I assemble a query that brings me the sales totals in the last 3 periods from the table 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’d need an exit like this:

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

Grateful for the help.

  • Have you ever tried anything, select * from tabela. If yes, edit your question so we can better help you.

1 answer

1


There’s nothing complicated about it:

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

See on Sqlfiddle

  • Grateful for the answer, however the table datas_peridos only has date information, the other table has sales information, I would like a query that brings sales from the intervals of the table data_periodos

  • But that’s what this query does! You went to see in Sqlfiddle?

  • But that’s what the query is doing

  • I’m sorry, I misconstrued, it really worked as expected, you really are very correct regarding the query,

  • very grateful, worked perfectly.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.