Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
jbon
Starting Member
20 Posts |
Posted - 2008-02-21 : 17:47:47
|
| Hi,I got 3 tables, VehicleType (VehicleTypeID is PK), Vehicle (VehicleID isPK, VehicleTypeID is FK) and Position (PositionID is PK, VehicleID is FK). Position table save all positions from all vehicles. I want from that table have latest position for all vehicles, how do I get this? PositionID is just automatic numbered 1, 2, 3, ... I use (so far) following sql query but it gives me all positions, I want only latest position for all vehicles. Can I use DateTime for having latest positions in some way? Or am I forced to use PositionID to get my wanted result?SELECT RegNo, AltID, VehicleType, DateTime, Speed, Latitude, Longitude From Vehicle v JOIN VehicleType vt ON v.VehicleTypeID = vt.VehicleTypeIDJoin Position p ON v.VehicleID = p.VehicleIDORDER BY DateTime DESCThanks in advance!/J |
|
|
jdaman
Constraint Violating Yak Guru
354 Posts |
Posted - 2008-02-21 : 17:54:21
|
quote: Originally posted by jbon Hi,I got 3 tables, VehicleType (VehicleTypeID is PK), Vehicle (VehicleID isPK, VehicleTypeID is FK) and Position (PositionID is PK, VehicleID is FK). Position table save all positions from all vehicles. I want from that table have latest position for all vehicles, how do I get this? PositionID is just automatic numbered 1, 2, 3, ... I use (so far) following sql query but it gives me all positions, I want only latest position for all vehicles. Can I use DateTime for having latest positions in some way? Or am I forced to use PositionID to get my wanted result?SELECT RegNo, AltID, VehicleType, DateTime, Speed, Latitude, Longitude From Vehicle v JOIN VehicleType vt ON v.VehicleTypeID = vt.VehicleTypeIDJoin Position p ON v.VehicleID = p.VehicleIDORDER BY DateTime DESCThanks in advance!/J
I dont see a datetime column anywhere in your table schema so either there are additional columns in the 3 tables that you arent specifying or your above query will not work.Going from what we have and assuming that your PositionID is sequential the following query will give you the latest position of each vehicle:SELECT MAX(PositionID), VehicleIDFROM PositionGROUP BY VehicleID |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-02-21 : 23:08:18
|
full query will be something likeSELECT RegNo, AltID, VehicleType, DateTime, Speed, Latitude, Longitude From Vehicle v JOIN VehicleType vt ON v.VehicleTypeID = vt.VehicleTypeIDJoin Position p ON v.VehicleID = p.VehicleIDJoin (SELECT MAX(PositionID) AS MaxPos, VehicleID FROM Position GROUP BY VehicleID )tmpON tmp.MaxPos=p.PositionIDAND tmp.VehicleID=v.VehicleID |
 |
|
|
jbon
Starting Member
20 Posts |
Posted - 2008-02-22 : 05:06:11
|
| Thx for your support. Yes I have not included all columns in my tables, you're right. I will make a try and see if your proposal give me the result I want. BTW, if I change to MAX(DateTime) instead of PositionID, that should work as well, or? Because that would be more correct I guess.My position table looks like:-PositionID, PK-VehicleID, FK-DateTime (foolish to use DateTime, I gues it's best change this column name)-Latitude-Longitude-Altitude-Direction-SpeedAgain, Thx. |
 |
|
|
jbon
Starting Member
20 Posts |
Posted - 2008-02-22 : 05:40:35
|
| I don't know if it's important... but I use MS SQL Server 2005 (Express edition) and T-SQL. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-02-22 : 06:32:12
|
| [code]SELECT RegNo, AltID, VehicleType, DateTime, Speed, Latitude, Longitude From Vehicle v JOIN VehicleType vt ON v.VehicleTypeID = vt.VehicleTypeIDJoin Position p ON v.VehicleID = p.VehicleIDJoin (SELECT MAX(DateTime) AS MaxDate, VehicleID FROM Position GROUP BY VehicleID )tmpON tmp.MaxDate=p.DateTimeAND tmp.VehicleID=v.VehicleID[/code] |
 |
|
|
jbon
Starting Member
20 Posts |
Posted - 2008-02-24 : 16:13:33
|
| Thx visakh16! It was an advanced one (at least for me) and it worked, great. What about performance, is it the most effective written sql query? Can I do something more to optimize this question and table, such as indexing, stored procedures, etc., as I shortly read about? |
 |
|
|
|
|
|
|
|