How to filter records by date using SQL

1

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?

    
asked by anonymous 20.12.2018 / 13:58

2 answers

2

Is it something like this you're looking for?

SELECT      TOP 1 *
FROM        Tabela
ORDER BY    DT_DATA DESC
    
20.12.2018 / 14:30
3

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;
    
20.12.2018 / 14:32