I am using SQL Server and need to query on a table the latest price record according to the date. Of all the records, I need him to bring me only 1 and with the current date. How do I do this?
Is it something like this you're looking for?
SELECT TOP 1 *
FROM Tabela
ORDER BY DT_DATA DESC
From the figure it is noticed that there are several lines for the same product.
The solution varies by version of SQL Server. Considering the latest information from a single product, we can have:
-- código #2
with Produto_ord as (
SELECT ID_PRODUTOEMPRESA, DT_DATA, VR_PRECO,
seq= row_number() over (order by DT_DATA desc)
from tabela
where ID_PRODUTOEMPRESA = ___
)
SELECT ID_PRODUTOEMPRESA, DT_DATA, VR_PRECO
from Produto_ord
where seq = 1;