SELECT NMC.*, Exam.Final_Exam_Level
FROM
(SELECT Technicians.Technician_ID AS Technician_ID,
Technicians.First_Name AS First_Name,
Technicians.Surname AS Surname,
MAX(New_Models.Date_Issued) AS Last_Course_Date,
MAX(New_Models.Issue) AS Last_Issue,
MAX(New_Models.Model_ID) AS Last_Model_ID,
Technicians.Course_Level AS No_Training_Courses
FROM New_Models, New_Models_Allocation, Technicians
WHERE New_Models.Model_ID=New_models_Allocation.Model_ID
And Technicians.Technician_ID=New_Models_Allocation.Technician_ID
GROUP BY Technicians.Technician_ID, Technicians.Course_Level, First_Name, Surname
ORDER BY MAX(New_Models.Model_ID) DESC)
AS NMC
INNER JOIN (SELECT Technicians.Technician_ID, COUNT(*) AS Final_Exam_Level
FROM Technicians, Exams, Exam_Allocation
WHERE (Technicians.Technician_ID)=Exam_Allocation.Technician_ID
And ((Exams.Exam_ID)=Exam_Allocation.Exam_ID)
And (Exams.Date_Taken)<=#12/31/2010#
GROUP BY Technicians.Technician_ID, Technicians.Course_Level
ORDER BY Technicians.Technician_ID)
AS Exam ON Exam.Technician_ID=NMC.Technician_ID;
此查询显示每位技师,上次考试,上次New_Model,上一课程。
SELECT Technicians.Technician_ID, Jobs.Job_ID, Jobs.Date_Occured, Fix
FROM Technicians, Jobs, Tech_Allocation, Recovery
WHERE Technicians.Technician_ID=Tech_Allocation.Technician_ID
And Jobs.Job_ID=Tech_Allocation.Job_ID
And Jobs.Job_ID=Recovery.Job_ID
And Jobs.Date_Occured>=#1/1/2010#
And Jobs.Date_Occured<=#12/31/2010#
ORDER BY Fix;
此查询显示每位技术人员已完成的工作。
但是,在Ms Access中创建报表时,会重复这些作业。因此,代替技术人员完成3个工作,例如显示12个。虽然在运行第二个查询本身时,结果不会重复。
任何帮助?
答案 0 :(得分:1)
由于显而易见的原因,我通常不会阅读其他人的SQL查询,但您的示例格式非常好。这是问题吗?
INNER JOIN (SELECT Technicians.Technician_ID, COUNT(*) AS Final_Exam_Level
...
GROUP BY Technicians.Technician_ID, Technicians.Course_Level
这两行来自您第一个查询的第二个子查询。您有1个索引字段(Technician_ID),但有2个分组字段(Technician_ID和Course_Level)。这将产生如下结果:
Technician_ID Final_Exam_Level
Bob 5
Bob 4
Nadine 5
我建议从Group By中删除Course_Level或将其添加到Select行。