SQL Select Where

0

Personal help please,

I need to make a select in a table that contains ID, VALUE, DATE, NAME.

The result has to bring only the lines where the date was the largest one.

Example:

ID    VALOR     DATA        NOME
1      100      22/05/2016  pedro
2      120      22/05/2016  lucas
3      100      21/05/2016  pedro
4      190      21/05/2016  lucas
5      100      21/05/2016  pedro
6      190      21/05/2016  lucas

RESULT:

D    VALOR     DATA        NOME
1      100      22/05/2016  pedro
2      120      22/05/2016  lucas
    
asked by anonymous 22.05.2016 / 19:37

2 answers

3

You can use the MAX function of SQL in your condition

SELECT *
FROM <tabela>
WHERE DATA = (SELECT MAX(DATA) FROM <tabela>)
    
22.05.2016 / 19:40
-1

You can make a subquery within a SELECT and use the MAX function that returns the oldest age. That is, you will do a WHERE where the comparison with a value will come from a SELECT query.

SELECT id, valor, data, nome
FROM Tabela
WHERE data = (SELECT MAX(data) FROM Tabela)
    
17.06.2016 / 22:11