How to select only 1 record per month with SQL

1

I have a sales table and would like to select customer sales, but if the customer made more than one sale per month, I would like it to bring the older one just, how could I do that?

I do not have SQL code to provide because I do not know where to start, but I have the table in question:

HowIwishitwerereturn:

Noticethatline2disappeared,ascustomer"John" made 2 purchases in January and brought only his oldest purchase.

I'm just studying, these tables are only fictitious.

    
asked by anonymous 07.05.2018 / 17:29

1 answer

2

As I understand it, if you want the minimum date value for month (which is the oldest purchase) for a particular name , example:

SELECT t1.id, 
    t1.nome,
    t1.data_compra, 
    t1.valor_compra 
FROM Compras t1
INNER JOIN (SELECT nome, MONTH(data_compra) as m, 
            MIN(data_compra) as c FROM Compras t2   
            GROUP BY nome, MONTH(data_compra)) AS t2 
  ON t1.nome = t2.nome AND t1.data_compra = t2.c

See example online

I made based on the two tables and with a INNER JOIN brought the values by name, month and date.

    
07.05.2018 / 17:51