Select per group

4

Good morning guys, I'd like to know how to do the following SELECT in mysql:

Having the following table, I need to select the last 3 purchase of each Name:

From now on, thanks

    
asked by anonymous 03.04.2017 / 14:45

1 answer

2

Rone,

One option is this:

SELECT A.* FROM COMPRAS A 
WHERE (SELECT COUNT(*) FROM COMPRAS B WHERE B.NOME = A.NOME AND B.DATA >= A.DATA) <= 3
ORDER BY NOME, DATA;
    
03.04.2017 / 22:03