S
ScottA
I'm trying to produce a report that will show when a
machine needs service, which is calculated by evaluating
data in two tables - a service records table and a service
schedule table. The business rule for calculating next
service is to check the schedule, which contains both a
start date and interval, and the records table which shows
the last maintenance. The next service should be the
later of (Start Date) or (Last Mainenance + Interval).
Each machine can be subject to multiptle types of
service/inspection.
I've been able to put figures from these two tables
together using a Union query that also eliminates null
values where they would cause problems:
ELECT tblServiceRecords.EqID,
tblServiceRecords.ServiceTypeID, DateAdd("m",
[tblServiceSchedule.Interval],[ServiceDate]) AS DueDate
FROM tblServiceRecords LEFT JOIN tblServiceSchedule ON
(tblServiceRecords.EqID = tblServiceSchedule.EqID) AND
(tblServiceRecords.ServiceTypeID =
tblServiceSchedule.ServiceTypeID)
WHERE (((tblServiceSchedule.Interval) Is Not Null))
UNION SELECT EqID, ServiceTypeID, StartDate FROM
tblServiceSchedule WHERE (tblServiceSchedule.StartDate) Is
Not Null;
The result is a record for all possible due dates
Now I would like to bring these results into another query
that shows a record for each type of service with the
latest due date. I'm working with a TOP 1 query, but it
doesn't seem to do the job right.
Specifically, it is asking me for two parameters
(tblServiceSchedule.ServiceTypeID and
tblServiceSchedule.EqID) that are both in the query!!!
SELECT tblServiceSchedule.EqID,
tblServiceSchedule.ServiceTypeID,
tblServiceSchedule.Interval,
tblServiceSchedule.ServiceCompanyID, (SELECT TOP 1 DueDate
FROM quniServiceDueDates WHERE (ServiceTypeID =
tblServiceSchedule.ServiceTypeID AND EqID =
tblServiceSchedule.EqID) ORDER BY DueDate DESC ) AS
ServiceDue
FROM tblServiceSchedule;
Can anybody tell me what I might have done wrong?
Thanks,
Scott A
machine needs service, which is calculated by evaluating
data in two tables - a service records table and a service
schedule table. The business rule for calculating next
service is to check the schedule, which contains both a
start date and interval, and the records table which shows
the last maintenance. The next service should be the
later of (Start Date) or (Last Mainenance + Interval).
Each machine can be subject to multiptle types of
service/inspection.
I've been able to put figures from these two tables
together using a Union query that also eliminates null
values where they would cause problems:
ELECT tblServiceRecords.EqID,
tblServiceRecords.ServiceTypeID, DateAdd("m",
[tblServiceSchedule.Interval],[ServiceDate]) AS DueDate
FROM tblServiceRecords LEFT JOIN tblServiceSchedule ON
(tblServiceRecords.EqID = tblServiceSchedule.EqID) AND
(tblServiceRecords.ServiceTypeID =
tblServiceSchedule.ServiceTypeID)
WHERE (((tblServiceSchedule.Interval) Is Not Null))
UNION SELECT EqID, ServiceTypeID, StartDate FROM
tblServiceSchedule WHERE (tblServiceSchedule.StartDate) Is
Not Null;
The result is a record for all possible due dates
Now I would like to bring these results into another query
that shows a record for each type of service with the
latest due date. I'm working with a TOP 1 query, but it
doesn't seem to do the job right.
Specifically, it is asking me for two parameters
(tblServiceSchedule.ServiceTypeID and
tblServiceSchedule.EqID) that are both in the query!!!
SELECT tblServiceSchedule.EqID,
tblServiceSchedule.ServiceTypeID,
tblServiceSchedule.Interval,
tblServiceSchedule.ServiceCompanyID, (SELECT TOP 1 DueDate
FROM quniServiceDueDates WHERE (ServiceTypeID =
tblServiceSchedule.ServiceTypeID AND EqID =
tblServiceSchedule.EqID) ORDER BY DueDate DESC ) AS
ServiceDue
FROM tblServiceSchedule;
Can anybody tell me what I might have done wrong?
Thanks,
Scott A