Click here to Skip to main content
15,887,477 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
How to get latest record with same ID in SQL Server


What I have tried:

How to get latest record with same ID in SQL Server
Posted
Updated 15-May-20 21:50pm

1 solution

Try:
SQL
SELECT TOP 1 * 
FROM MyTable 
WHERE Id = 12345 
ORDER BY MyTimestampColumn DESC
 
Share this answer
 
Comments
Boopalslm 16-May-20 4:48am    
I am use this query already, but I want I am entered ID no -1 10 bills and ID no-2 entered 8 bills
Here I want how to get 1st id last record(10th Bill), and 2nd id –last record(8th Bill), give me some suggestion.
OriginalGriff 16-May-20 4:57am    
What do you store in your DB that provides a "first" and "last" record indicator for each ID? If you don't store anything, then you can't order records by anything reliably!

If you do, and you want the "last" value for each ID, then you need to look at GROUP BY and JOIN to return you multiple ID rows, with the "last" entry for each.

But we have no idea of your DB design, or any access to your data, so you need to look at your design and work out what part fits what.

This may help for the GROUP BY clause:
https://www.codeproject.com/Articles/1110163/SQL-GROUP-By-and-the-Column-name-is-invalid-in-the
Boopalslm 16-May-20 5:14am    
yes sir, i want above your details, i will try my DB.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900