I have a sql server 2012 database, where I have a table that contains a datetime field, and what I need is to obtain the last insert that was made, I have the following table and I hope you can help me
----------------------------------------------------------------------
|cod_reporte|nombre_ruta |email |tipo |descripcion |fecha |
----------------------------------------------------------------------
| int | varchar |varchar |varchar |varchar |datetime |
-----------------------------------------------------------------------
what I need exactly is to get the last insert, specifying a path
example: route 1= ..... .....
route 2= ..... ....
I hope you can help me
you can get it with
This will bring you the last
Id
insertedWhat I understand is that for a particular route you want to return the record with the most recent date. If this is what you want, you can use
TOP 1
combined with aORDER BY
by columnfecha
.Here I leave you other options that you can also use using subqueries, or the window function
row_number()
. But the option withTOP 1
it is the simplest:Try something like this:
With this query you get a record
TOP 1
of YOUR_TABLE where the ROUTE_NAME is equal to the route name you need, which is ordered by the descending signature DATE, from highest to lowest.