2011-05-01 54 views
1
SELECT Student.S_ID, Student.First_Name, Student.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, 
     Student.Course_Level AS No_Training_Courses, 
     **COUNT(Exam_Student.Exam_ID) AS Final_Exam_Level** 
FROM New_Models, Model_Student, Student, Exams, Exam_Student 
WHERE New_Models.Model_ID=Model_Student.Model_ID 
    AND Student.S_ID=Model_Student.S_ID 
    AND (Student.S_ID)=Exam_Student.S_ID 
    AND ((Exams.Exam_ID)=Exam_Student.Exam_ID) 
    AND (Exams.Date_Taken)<=#12/31/2010# 
GROUP BY Student.S_ID, First_Name, Surname, S.Course_Level 
ORDER BY Student.S_ID, MAX(New_Models.Model_ID) DESC; 

除COUNT命令外,所有結果都顯示爲需要。當與實際結果比較時,只有第一個細胞不同而且是錯誤的。 它應該計算學生完成了多少次考試。查詢中的錯誤

我可以回答任何問題,將等待你的幫助。

SELECT 
    Student.S_ID, 
    Student.first_name, 
    Student.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, 
    Student.course_level AS no_training_courses 
FROM 
    new_models, 
    Student, 
    Model_Student 
WHERE 
    new_models.model_id = Model_Student.model_id 
    AND Student.S_id = Model_Student.S_id 
    AND *strong text*Exam_ID IN ( 
      SELECT COUNT(Exam_Student.Exam_id) AS Final_Exam_Level 
      FROM 
       Exams, 
       Exam_Student, 
       Student 
      WHERE 
       Student.S_ID = Exam_Student.S_ID 
       AND exams.date_taken <=#12/31/2010# 
      Group by Student.S_ID) 
GROUP BY 
    Student.S_ID, 
    first_name, 
    surname, 
    Student.Course_level 
ORDER BY 
    Student.S_id, 
    MAX(new_models.model_id) DESC; 

我不能使用INNER JOIN,所以我想知道類似上面的東西是可以做到的。但是,我不知道如何做子查詢!

+0

那麼到底爲什麼你不能使用內部連接? – 2011-05-02 21:10:28

回答

0

我猜沒有看到示例數據,但也許試試這個,改變

AND (Student.S_ID)=Exam_Allocation.S_ID 
AND ((Exams.Exam_ID)=Exam_Allocation.Exam_ID) 

AND Student.S_ID=Exams.Exam_ID 
AND Exams.Exam_ID=Exam_Allocation.Exam_ID 

如果這沒有幫助,您將需要發佈一些示例數據預期與實際有人提供幫助的結果。

+0

它沒有幫助,我認爲所有的連接都是正確的。任何其他建議。 – Qatrelnada 2011-05-01 17:54:48

+0

只是其中一個計數結果是錯誤的 – Qatrelnada 2011-05-01 18:09:05

3

可能有幫助的一種方法是簡單地在Counts上創建子查詢,然後加入到該查詢中。

我還建議使用ANSI-92式的聯接

SELECT student.s_id, 
     student.first_name, 
     student.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, 
     student.course_level  AS no_training_courses, 
     Examqry.ExamCount 
FROM new_models 
     INNER JOIN model_student ON new_models.model_id = model_student.model_id 
     INNER JOIN student ON student.s_id = model_student.s_id 
     INNER JOIN (SELECT COUNT(Exam_id) ExamCount , 
             s_id 
          FROM exams Group by s_id 
          Where exams.date_taken <=#12/31/2010#) examqry 
     ON student.s_id = examqry.s_id 

GROUP BY student.s_id, 
      first_name, 
      surname, 
      s.course_level 
ORDER BY student.s_id, 
      MAX(new_models.model_id) DESC; 
+0

你在哪裏得到了SELECT中的examcount.examcount? – Qatrelnada 2011-05-01 18:12:19

+1

@qartrelana它來自也稱爲內聯視圖的查詢。我更新了答案,使其更清晰一些。 Examqry是內聯視圖的別名。考試計數是exam_id計數的別名。另外我的地方是在錯誤的地方 – 2011-05-01 19:57:20

+0

我試着用你的方法,但我不能使用內部連接。但是,我這樣做的方式現在不工作了你可以檢查嗎? SELECT Student.S_ID,Student.first_name,Student.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, – Qatrelnada 2011-05-02 18:25:30