2011-05-02 100 views
0
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,最後一道菜。創建報表,重複的報表輸出,女士訪問SQL

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

(這與您的問題沒有直接關係,但仍然可能是問題)。我看到爲了捕獲2010年的所有數據,你使用了'<#12/31/2010#'。但是,如果這些日期欄也存儲時間部分,則排除午夜後發生在12/31/2010的所有事件。我建議使用'<#01/01/2011#'代替。合理? – PowerUser 2011-05-03 12:46:20

回答

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行中。