我有这个问题:http://pastebin.com/JNx1hX1i
我想在我的查询中添加新的2列.OldLat(5分钟之前的Lat),OldLat(5分钟前的Lng)
结果就像这样
LatOfTruck LngOfTruck SpeedOfTruck LastTime PlateOfTruck
36.9573 27.8099 72 11.12.2013 12:30:00 123456
34.9573 27.5053 82 11.12.2013 11:30:00 541456
38.8952 37.7855 52 11.12.2013 10:30:00 78455
. . . . .
. . . . .
. . . . .
查询:
SELECT LatOfTruck = TruckLocation.Lat,
LngOfTruck = TruckLocation.Lng,
SpeedOfTruck = TruckLocation.Speed,
LastTime = CONVERT(VARCHAR(10), [TruckLocation.ReadTime], 104) +
' ' + CONVERT(VARCHAR(8), [TruckLocation.ReadTime], 108),
PlateOfTruck = Trucks.Plate
FROM TruckLocation
JOIN Truck AS Trucks
ON Trucks.OID = TruckLocation.TruckID
WHERE TruckLocation.OID
IN (SELECT MAX(TruckLocation_1.OID) AS OID
FROM TruckLocation AS TruckLocation_1
JOIN Truck
ON TruckLocation_1.TruckID = Truck.OID
GROUP BY TruckLocation_1.TruckID)
ORDER BY TruckLocation.ReadTime DESC
将OldLat和OldLng结果添加为:
LatOfTruck LngOfTruck SpdOfTruck LastTime PlateOfTruck OldLat OldLng
36.9573 27.8099 72 date 123456 LatOf5mAgo LngOf5mAgo
34.9573 27.8099 82 date 121456 LatOf5mAgo LngOf5mAgo
38.8952 37.7855 52 date 78455 LatOf5mAgo LngOf5mAgo
. . . . . . .
. . . . . . .
. . . . . . .
答案 0 :(得分:0)
如果您的OldLat和OLng已修复,则可以将两者添加为
Select LatOfTruck, LngOfTruck,OldLat='LatOf5mAgo', OldLng='LngOf5mAgo'
From TbaleName
我希望你明白我的观点。不是一个确切的,但肯定会给你一个想法。以下是您的原始查询。
SELECT LatOfTruck=TruckLocation.Lat,LngOfTruck=TruckLocation.Lng,
SpeedOfTruck=TruckLocation.Speed,LastTime=CONVERT(VARCHAR(10), [TruckLocation.ReadTime], 104) + ' ' + CONVERT(VARCHAR(8),
[TruckLocation.ReadTime],108)
,PlateOfTruck=Trucks.Plate,OldLat='LatOf5mAgo', OldLng='LngOf5mAgo'
FROM TruckLocation
JOIN Truck AS Trucks ON Trucks.OID=TruckLocation.TruckID
WHERE TruckLocation.OID IN ( SELECT MAX(TruckLocation_1.OID) AS OID
FROM TruckLocation AS TruckLocation_1
JOIN Truck ON TruckLocation_1.TruckID = Truck.OID
GROUP BY TruckLocation_1.TruckID
)
ORDER BY TruckLocation.ReadTime DESC