Chelle1661
Registered User.
- Local time
- Today, 19:42
- Joined
- Feb 6, 2013
- Messages
- 26
RUNNING DIFFERENCE PROBLEM:
I am an amateur Access user and trying to get a running difference into a query based on a simple vehicle table and a vehicle readings table. This query for a report which will show the daily difference in the odometer readings. The table lists multiple vehicles in a fleet and a reading every day for each of these vehicles. I need to be specific about the previous reading i.e. pull through the previous day's reading for that specific vehicle, not the multiple values [of the fleet] for that particular day. I have been troubleshooting this and trying to find a solution on line [and I may now have a total jumble in my select query]! Please help if you can as I keep getting syntax or operator error messages. Below is what I have in my SQL view showing my attempts so far - as you can see I have not referenced the VehicleID adequately [which will ensure that the running difference pulls through the one relevant value], and I am not sure where to place the sub query in the main query:


SELECT Vehicles.VehicleSortOrder, Vehicles.Vehicle, VehicleReadings.VehicleReadingDate, Vehicles.VehicleCategoryID, VehicleReadings.VehicleReading, VehicleReadings.VehicleLitres, VehicleReadings.OperationsDayID, Vehicles.VehicleID, VehicleReadings.VehicleReadingID, Vehicles.VehicleNumber, Vehicles.VehicleOperationTypeID, Vehicles.OpsAreaID, Vehicles.VehicleUpdateDate
(RunningDifference: SELECT VehicleReadingID, VehicleReadings.VehicleReading, VehicleReading - nz((SELECT VehicleReading from VehicleReadings WHERE VehicleReadingDate =
(SELECT max(VehicleReadingDate) FROM VehicleReadings where VehicleReadingDate < T.VehicleReadingDate))) AS RunningDiff FROM VehicleReadings as T order by VehicleReadingDate)
FROM Vehicles RIGHT JOIN VehicleReadings ON Vehicles.VehicleID = VehicleReadings.VehicleID;
I am an amateur Access user and trying to get a running difference into a query based on a simple vehicle table and a vehicle readings table. This query for a report which will show the daily difference in the odometer readings. The table lists multiple vehicles in a fleet and a reading every day for each of these vehicles. I need to be specific about the previous reading i.e. pull through the previous day's reading for that specific vehicle, not the multiple values [of the fleet] for that particular day. I have been troubleshooting this and trying to find a solution on line [and I may now have a total jumble in my select query]! Please help if you can as I keep getting syntax or operator error messages. Below is what I have in my SQL view showing my attempts so far - as you can see I have not referenced the VehicleID adequately [which will ensure that the running difference pulls through the one relevant value], and I am not sure where to place the sub query in the main query:



SELECT Vehicles.VehicleSortOrder, Vehicles.Vehicle, VehicleReadings.VehicleReadingDate, Vehicles.VehicleCategoryID, VehicleReadings.VehicleReading, VehicleReadings.VehicleLitres, VehicleReadings.OperationsDayID, Vehicles.VehicleID, VehicleReadings.VehicleReadingID, Vehicles.VehicleNumber, Vehicles.VehicleOperationTypeID, Vehicles.OpsAreaID, Vehicles.VehicleUpdateDate
(RunningDifference: SELECT VehicleReadingID, VehicleReadings.VehicleReading, VehicleReading - nz((SELECT VehicleReading from VehicleReadings WHERE VehicleReadingDate =
(SELECT max(VehicleReadingDate) FROM VehicleReadings where VehicleReadingDate < T.VehicleReadingDate))) AS RunningDiff FROM VehicleReadings as T order by VehicleReadingDate)
FROM Vehicles RIGHT JOIN VehicleReadings ON Vehicles.VehicleID = VehicleReadings.VehicleID;
Last edited: