SQL Select Where

Asked

Viewed 204 times

0

I need to make a SELECT in a table containing ID, VALOR, DATA and NOME.

The result has to bring only the lines on which the was the biggest date existing.

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

Expected result:

ID    VALOR        DATA        NOME
1      100      22/05/2016     pedro
2      120      22/05/2016     lucas
  • Thank you very much Carlos, I had tried something similar but something was wrong...

2 answers

8

You can use the function MAX SQL in its condition

SELECT *
FROM <tabela>
WHERE DATA = (SELECT MAX(DATA) FROM <tabela>)

-1

You can do a sub-query within a SELECT and use the MAX function that returns to adulthood. 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)
  • Regilan, try to bring different answers to add more knowledge, your answer was probably negative, even if it was the same as before, to discourage duplicated information. I did not deny your question, I’m just giving this touch to improve the level of the forum. Hug!

Browser other questions tagged

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