Search recordset by date

0

I'm currently creating a Java / Mysql Desktop program. When inserting in the database, the table is very simple:

id - dataInserido - códigoProduto - qtd

I would like, by means of a SQL, to group the last records entered on the same date in descending order. Example: I have attached product dock

id - data - produto - qtd  
1 - 20/05/2017 - 10001 - 100  
1 - 24/05/2017 - 10002 - 10  
1 - 27/05/2017 - 100010 - 30  
1 - 29/05/2017 - 100020 - 60  
1 - 29/05/2017 - 100060 - 70  
1 - 29/05/2017 - 100010 - 100  

How to group the last (05/29/2017), being 100020, 100060 and 100010, since I have to mount this table in Java? A kind of filter requested by the user.

ps: I do not want to take any date by java, but rather a query by sql itself.

    
asked by anonymous 29.05.2017 / 14:16

1 answer

1

What you need to do is filter through the data column using the WHERE clause with NOT EXISTS :

SELECT t.id,
       t.data,
       t.produto,
       t.qtd
  FROM tabela t
 WHERE NOT EXISTS(SELECT 1
                    FROM tabela t2
                   WHERE t2.data > t.data)
 ORDER BY t.produto DESC

The ORDER BY clause will sort the records according to the column that was specified.

Note that I respected the column names given in the example and not in its description.

    
29.05.2017 / 14:26