Click here to Skip to main content
15,850,545 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi can anybody tell me how to get the latest data logged in database while sending the query. Like i have MS Access database in which i have logged data along with time-stamping, so when i send a query i should get only the latest information stored in it or get the latest data from it based on the last recorded time.

The following is my database data:

DeviceName Window DateTime

Device1 win1 6/10/2013 9:40:57
Device1 win2 6/10/2013 9:40:57

Device1 win1 6/11/2013 9:39:39
Device1 win2 6/11/2013 9:39:39

Device1 win1 6/11/2013 10:39:39
Device1 win2 6/11/2013 10:39:39
Device1 win3 6/11/2013 10:39:40
Device1 win4 6/11/2013 10:39:40
Device1 win5 6/11/2013 10:39:40

As you can see device1 is logged @ different intervals & on different days, but from these logged data i need only the latest logged data i.e, the lower 4 values of the table logged @ 10:39:39 till 10:39:40 & on 6/11/2013. How do i write a query for this case?
Updated 10-Jun-13 21:21pm

1 solution

You can try querying the table using Top and Order by clause to get the latest 4 records. something like this

SELECT TOP 5 [DeviceName], [WindowName], [LogTime]
Share this answer
Jagadisha_Ingenious 11-Jun-13 5:04am    
@john-pH: Hi john thanks for replying & i have implemented the query you have posted & it works fine, but what if the device1 shows up only 3 or more faults. As per your query i will get only the top 5 data's from the database. If it has more or less than 5 how to get all those log details. Since this logging is dynamic.

Thanks in advance.
John-ph 11-Jun-13 5:24am    
TOP is to indicate the number the latest records you want. To get all the log details just query without TOP. like
SELECT fields FROM TABLE ORDER BY DESC. That should do.

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