Click here to Skip to main content
15,890,282 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have the two tables
1)Vechile Detials
2)Tracking table

Can any one help me how can i fetch the latest records of all the vehicles.

What I have tried:

select t.IMEINo,t.speed,t.Ignition,t.TrackTime,tblSATVehicles.VehicleNumber
from  Tracking t
inner join tblSATVehicles on t.IMEINo=tblSATVehicles.DeviceIMEI
inner join (
    select IMEINo, max(TrackTime) as maxtime
    from Tracking tm
    group by IMEINo
) tm on t.IMEINo = tm.IMEINo and t.TrackTime= tm.maxTime
Posted
Updated 27-Dec-18 7:31am

The only reliable way to fetch the latest data is to timestamp rows when you INSERT or UPDATE them: you can then use ORDER BY timeStampColumn DESC to get then in the right order, and TOP 1 to select just the latest item.

If you don't have a timestamp, then you cannot guarantee ordering - and that means your DB can return rows in any order it finds most appropriate. Which doesn't have to be the same order two times in a row!
 
Share this answer
 
v3
Comments
MadMyche 27-Dec-18 14:27pm    
Hopefully the OP's t.TrackTime value meets that requirement.
Maybe you can use the OUTPUT clause, but this does not work with a select statement:
OUTPUT Clause (Transact-SQL) - SQL Server | Microsoft Docs[^]
 
Share this answer
 

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