开发者

combine two SQL queries

开发者 https://www.devze.com 2023-03-02 14:14 出处:网络
SELECT Student.S_ID, COUNT(*) AS **Final_Exam_Level** FROM Student, Exams, Exam_Alloca开发者_Python百科tion
SELECT Student.S_ID, COUNT(*) AS **Final_Exam_Level**
FROM Student, Exams, Exam_Alloca开发者_Python百科tion
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;

How can I add Final_Exam_Level into the second query?

Final_Exam_Level is counting how many Exam_ID did each Student do. Exam_Allocation has two foreign keys, S_Id and 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 ;


Just turn both of them into sub-queries of a higher query

 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

精彩评论

暂无评论...
验证码 换一张
取 消