L
Loni - RWT
I have created a database to keep mileage/trip records for a small trucking
company. Each trip (tblTrips) can have multiple entries (tblTripDetails)
with mileage descriptions such as crossing state lines, loading/unloading,
fueling, and trip end. I had a set of cascading queries that first pulled
all odometer entries with a mileage description of "trip end" for a specific
truck. It then selected the maximum odometer entry from those records and
returned the matching location and state. That worked fine until the
occasion arose when a truck had to be towed by a wrecker. We added an
additional mileage description, "wrecker," to handle this. In that instance,
we had a "trip end" mileage description with an odometer entry at one
location and a seperate trip with a "wrecker" mileage description at a
different location, but the same odometer. I modified my queries to include
"wrecker" along with "trip end." That appeared to work, as it pulled all
odometer entries with those mileage descriptions. However, when it selects
the maximum odometer entry from those records, it is returning both "trip
end" and "wrecker" since they have the same odometer. It is highly possible
(in this instance it happened) that the "trip end" and the "wrecker" trips
occured on the same date. The one unique identifier is the TripDetailsID.
When I try to pull the max of these, it still returns both records. How do I
establish a criteria to separate the two?
Thanks in advance for your help!
Posted below is the SQL for these queries.
qryTripEnd1:
SELECT tblTrips.TripID, tblTrips.TripDate, tblTrips.TruckID,
tblTripDetails.MileageDescription, tblTripDetails.Odometer
FROM tblTrips INNER JOIN tblTripDetails ON tblTrips.TripID =
tblTripDetails.TripID
WHERE (((tblTrips.TripID)<[Forms]![frmTrips]![TripID]) AND
((tblTrips.TripDate)<=[Forms]![frmTrips]![TripDate]) AND
((tblTrips.TruckID)=[Forms]![frmTrips]![truckID]) AND
((tblTripDetails.MileageDescription)="Trip End")) OR
(((tblTrips.TripID)<[Forms]![frmTrips]![TripID]) AND
((tblTrips.TripDate)<=[Forms]![frmTrips]![TripDate]) AND
((tblTrips.TruckID)=[Forms]![frmTrips]![truckID]) AND
((tblTripDetails.MileageDescription)="Wrecker"))
ORDER BY tblTripDetails.Odometer;
qryTripEnd2:
SELECT qryTripEnd1.TruckID, qryTripEnd1.MileageDescription,
Max(qryTripEnd1.Odometer) AS MaxOfOdometer
FROM qryTripEnd1 INNER JOIN tblTripDetails ON qryTripEnd1.Odometer =
tblTripDetails.Odometer
GROUP BY qryTripEnd1.TruckID, qryTripEnd1.MileageDescription;
qryTripEnd2A:
SELECT qryTripEnd2.MaxOfOdometer, qryTripEnd2.MileageDescription,
tblTripDetails.TripID, tblTripDetails.TripDetailsID
FROM qryTripEnd2 INNER JOIN tblTripDetails ON
(qryTripEnd2.MileageDescription = tblTripDetails.MileageDescription) AND
(qryTripEnd2.MaxOfOdometer = tblTripDetails.Odometer);
qryTripEnd2B:
SELECT qryTripEnd2A.MaxOfOdometer, qryTripEnd2A.TripID,
Max(qryTripEnd2A.TripDetailsID) AS MaxOfTripDetailsID, tblTrips.TripDate
FROM qryTripEnd2A INNER JOIN tblTrips ON qryTripEnd2A.TripID = tblTrips.TripID
GROUP BY qryTripEnd2A.MaxOfOdometer, qryTripEnd2A.MileageDescription,
qryTripEnd2A.TripID, tblTrips.TripDate;
qryTripEnd3:
SELECT qryTripEnd2B.MileageDescription, tblTripDetails.Odometer,
tblTripDetails.TripDetailsID, tblTripDetails.Location, tblTripDetails.State
FROM tblTripDetails INNER JOIN qryTripEnd2B ON (qryTripEnd2B.TripID =
tblTripDetails.TripID) AND (tblTripDetails.MileageDescription =
qryTripEnd2B.MileageDescription) AND (tblTripDetails.Odometer =
qryTripEnd2B.MaxOfOdometer);
company. Each trip (tblTrips) can have multiple entries (tblTripDetails)
with mileage descriptions such as crossing state lines, loading/unloading,
fueling, and trip end. I had a set of cascading queries that first pulled
all odometer entries with a mileage description of "trip end" for a specific
truck. It then selected the maximum odometer entry from those records and
returned the matching location and state. That worked fine until the
occasion arose when a truck had to be towed by a wrecker. We added an
additional mileage description, "wrecker," to handle this. In that instance,
we had a "trip end" mileage description with an odometer entry at one
location and a seperate trip with a "wrecker" mileage description at a
different location, but the same odometer. I modified my queries to include
"wrecker" along with "trip end." That appeared to work, as it pulled all
odometer entries with those mileage descriptions. However, when it selects
the maximum odometer entry from those records, it is returning both "trip
end" and "wrecker" since they have the same odometer. It is highly possible
(in this instance it happened) that the "trip end" and the "wrecker" trips
occured on the same date. The one unique identifier is the TripDetailsID.
When I try to pull the max of these, it still returns both records. How do I
establish a criteria to separate the two?
Thanks in advance for your help!
Posted below is the SQL for these queries.
qryTripEnd1:
SELECT tblTrips.TripID, tblTrips.TripDate, tblTrips.TruckID,
tblTripDetails.MileageDescription, tblTripDetails.Odometer
FROM tblTrips INNER JOIN tblTripDetails ON tblTrips.TripID =
tblTripDetails.TripID
WHERE (((tblTrips.TripID)<[Forms]![frmTrips]![TripID]) AND
((tblTrips.TripDate)<=[Forms]![frmTrips]![TripDate]) AND
((tblTrips.TruckID)=[Forms]![frmTrips]![truckID]) AND
((tblTripDetails.MileageDescription)="Trip End")) OR
(((tblTrips.TripID)<[Forms]![frmTrips]![TripID]) AND
((tblTrips.TripDate)<=[Forms]![frmTrips]![TripDate]) AND
((tblTrips.TruckID)=[Forms]![frmTrips]![truckID]) AND
((tblTripDetails.MileageDescription)="Wrecker"))
ORDER BY tblTripDetails.Odometer;
qryTripEnd2:
SELECT qryTripEnd1.TruckID, qryTripEnd1.MileageDescription,
Max(qryTripEnd1.Odometer) AS MaxOfOdometer
FROM qryTripEnd1 INNER JOIN tblTripDetails ON qryTripEnd1.Odometer =
tblTripDetails.Odometer
GROUP BY qryTripEnd1.TruckID, qryTripEnd1.MileageDescription;
qryTripEnd2A:
SELECT qryTripEnd2.MaxOfOdometer, qryTripEnd2.MileageDescription,
tblTripDetails.TripID, tblTripDetails.TripDetailsID
FROM qryTripEnd2 INNER JOIN tblTripDetails ON
(qryTripEnd2.MileageDescription = tblTripDetails.MileageDescription) AND
(qryTripEnd2.MaxOfOdometer = tblTripDetails.Odometer);
qryTripEnd2B:
SELECT qryTripEnd2A.MaxOfOdometer, qryTripEnd2A.TripID,
Max(qryTripEnd2A.TripDetailsID) AS MaxOfTripDetailsID, tblTrips.TripDate
FROM qryTripEnd2A INNER JOIN tblTrips ON qryTripEnd2A.TripID = tblTrips.TripID
GROUP BY qryTripEnd2A.MaxOfOdometer, qryTripEnd2A.MileageDescription,
qryTripEnd2A.TripID, tblTrips.TripDate;
qryTripEnd3:
SELECT qryTripEnd2B.MileageDescription, tblTripDetails.Odometer,
tblTripDetails.TripDetailsID, tblTripDetails.Location, tblTripDetails.State
FROM tblTripDetails INNER JOIN qryTripEnd2B ON (qryTripEnd2B.TripID =
tblTripDetails.TripID) AND (tblTripDetails.MileageDescription =
qryTripEnd2B.MileageDescription) AND (tblTripDetails.Odometer =
qryTripEnd2B.MaxOfOdometer);