2011-05-02 246 views
0
SELECT Student.S_ID, COUNT(*) AS **Final_Exam_Level** 
FROM Student, Exams, Exam_Allocation 
WHERE (Student.S_ID)=Exam_Allocation.S_ID 
    And ((Exams.Exam_ID)=Exam_Allocation.Exam_ID) 
    And (Exams.Date_Taken) <= #12/31/2010# 
GROUP BY Student.S_ID, Student.Course_Level 
ORDER BY Student.S_ID; 

SELECT Student.S_ID AS S_ID, Student.First_Name AS First_Name, Student.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, Student.Course_Level AS No_Training_Courses 
FROM New_Models, New_Models_Allocation, Student 
WHERE New_Models.Model_ID=New_models_Allocation.Model_ID And Student.S_ID=New_Models_Allocation.S_ID 
GROUP BY Student.S_ID, Student.Course_Level, First_Name, Surname 
ORDER BY MAX(New_Models.Model_ID) DESC; 

如何添加Final_Exam_Level到第二個查詢?合併兩個SQL查詢

Final_Exam_Level正在統計每個學生做了多少個Exam_ID。 Exam_Allocation有兩個外鍵,S_ID和Exam_ID

select Query_New_Models.*, Query_Exam.Final_Exam_Level 
FROM (SELECT Student.S_ID AS S_ID, Student.First_Name AS First_Name, Student.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, Student.Course_Level AS No_Training_Courses 
FROM New_Models, New_Models_Allocation, Student 
WHERE New_Models.Model_ID=New_models_Allocation.Model_ID And Student.S_ID=New_Models_Allocation.S_ID 
GROUP BY Student.S_ID, Student.Course_Level 
ORDER BY MAX(New_Models.Model_ID) DESC)    INNER JOIN   (SELECT  Student.S_ID, COUNT(*) AS Final_Exam_Level 
FROM Student, Exams, Exam_Allocation 
WHERE (Student.S_ID)=Exam_Allocation.S_ID 
And ((Exams.Exam_ID)=Exam_Allocation.Exam_ID) 
And (Exams.Date_Taken)<=#12/31/2010# 
GROUP BY Student.S_ID, Student.Course_Level 
ORDER BY Student.S_ID 
) ON Query_Exam.S_ID = Query_New_Models.S_ID ; 
+1

對不起的子查詢,但這些查詢返回一個(非常)不同的列數。 ..那麼你怎麼能融合他們? – Marco 2011-05-02 21:00:20

+0

如果您陳述您想要實現什麼目標,可能會更容易,除了「合併這兩個查詢」。結果數據集應該是什麼樣子? – FrustratedWithFormsDesigner 2011-05-02 21:03:01

+0

我想添加一列到另一列。儘管如此,他們擁有相同的編號或記錄。他們都展示了技術人員的詳細信息 – Qatrelnada 2011-05-02 21:03:44

回答

2

只要打開他們都進入一個更高的查詢

select Second.*, First.Final_Exam_Level 
    from (Your Second Query Here) Second 
      inner join 
     (Your First Query Here) First on First.Technician_ID = Second.Technician_ID 
+0

我不認爲他們都需要派生表,你應該能夠將第一個查詢(作爲派生表)直接加入到第二個 – 2011-05-02 21:13:02

+0

我試圖從第一個字段包含到第第二次,但它顯示錯誤的結果。 – Qatrelnada 2011-05-02 21:17:03

+0

你能否請我檢查一下從我瞭解你所說的內容後添加到我的文章中的SQL。對不起,但我不太瞭解SQL。非常感謝您的幫助 – Qatrelnada 2011-05-02 21:17:50