15,961,997 members
0.00/5 (No votes)
See more:
Hi,

In a table i am having some records. out of those records, two records having simillar data but only difference in that two records is Record date.

Now i want to retrive the latest record of those 2 records and following the other records.

Adcvice a query for this.

Thanks in Advance
Subhash G.
Posted
Updated 20-Jul-11 21:43pm
v3

## Solution 1

`select top 1 * from tablename order by recorddate desc`

Comments
subhash04573 21-Jul-11 2:00am
I have to show only latest record not all records.
RaviRanjanKr 21-Jul-11 2:06am
hey! its will give you only last records. are you not getting SQL query using top 1*. so how its will give you multiple records.
RaviRanjanKr 21-Jul-11 2:07am
If you are looking for something else so please be mention in your Question :)
m@dhu 21-Jul-11 2:47am
Did you try that query gives only one record.
RaviRanjanKr 21-Jul-11 2:02am
Nice Answer, My 5+

## Solution 2

You need to sort the results using `order by` clause. The sort order will be descending as you need the newest at the top. So the query will be like:

SQL
`SELECT * FROM TABLE_NAME ORDER BY DATE_COLUMN_NAME DESC`

This will give you all the results. Now filter the rows using `top` clause.

Hope this helps!

v2
Comments
subhash04573 21-Jul-11 2:00am
I have to show only latest record not all records.
Thnks
Ankur\m/ 21-Jul-11 2:21am
Did I not mention that you can filter the no. of rows using TOP clause?
RaviRanjanKr 21-Jul-11 2:03am
Nice Answer, My 5+
Ankur\m/ 21-Jul-11 2:21am
Thanks Ravi!

## Solution 4

Hi Subhash,

Find my query:
`select * from tablename where recorddate =(select max(recorddate) from tablename)`

This will return only 1 row.I think this will help you.

Cheers

-Sugato

## Solution 5

Do u have any auto increment field???
If so then try
`Select * from TableName where Id=(Select Max(Id) from TableName)`

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

Top Experts
Last 24hrsThis month
 OriginalGriff 40 RickZeeland 15 Pete O'Hanlon 10 Member 16331880 -6
 OriginalGriff 218 Pete O'Hanlon 150 merano99 80 den2k88 50 Andre Oosthuizen 50

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