使用Sql Query进行完全外连接

时间:2016-08-15 05:12:18

标签: sql sql-server

我遇到了Sql查询的问题。所以我首先发布表结构:

表格结构 - StudentGrade

ID - Department - StudentID - Grade - Course
--------------------------------------------
 1      2            2002      A+      102
 2      2            2002      B+      102
 3      2            2002      A+      104

我应该得到的输出如下:

ID - Department - StudentID - Grade - Course
----------------------------------------------
 2      2           2002        B+     102    // as this was inserted last
 3      2           2002        A+     104

我使用以下查询获得了上述结果,该查询仅在StudentGrade表中存储结果时才能正常工作:

SELECT 
    COALESCE(m.Grade, 'Not Graded Yet') AS Grade 
FROM 
    StudentGrade m
FULL OUTER JOIN 
    EnrollCourse k ON k.Course = m.Course
WHERE 
    m.ID IN (SELECT MAX(m.ID) 
             FROM StudentGrade m
             GROUP BY m.StudentID, m.Course) 
    AND m.StudentID = 2002    // this query returns the grade that is inserted lastly for a course and for a specific student 

顺便说一句,EnrollCourse的表格结构如下:

ID - StudentID - Course - EnrollDate
-------------------------------------
 1      2002       102    NULL
 2      2002       104    NULL

结果符合注册学生的条件,我的意思是参加课程的学生。所以现在我的要求是,假设,StudentID 2002已注册两门课程。如果没有为学生分配成绩,则输出 我希望从StudentGrade表:

ID - Department - StudentID - Grade -          Course
------------------------------------------------------
 1        2          2002     No Grade Yet       102
 2        2          2002     No Grade Yet       104

如果分配给课程102,那么它将如下:

ID - Department - StudentID - Grade -       Course
---------------------------------------------------
 1       2           2002     B+              102
 2       2           2002     No Grade Yet    104

同样,如果课程成绩更新为A +,那么它将是:

ID - Department - StudentID - Grade -          Course
------------------------------------------------------
 1        2          2002     A+                 102
 2        2          2002     No Grade Yet       104

查询中有一些INNER JOIN。所以我尝试使用以下内容(刚刚修改了上面似乎不起作用的内容):

SELECT 
    COALESCE(m.Grade, 'Not Graded Yet') AS Grade 
FROM 
    StudentGrade m
FULL OUTER JOIN 
    EnrollCourse k ON k.Course = m.Course
WHERE 
    m.ID IN (SELECT MAX(m.ID) 
             FROM StudentGrade m
             FULL OUTER JOIN EnrollCourse k ON k.Course = m.Course
             GROUP BY m.StudentRegNo, m.Course) 
    AND m.StudentID = 2002

1 个答案:

答案 0 :(得分:1)

首先,您需要在EnrollCourse中使用Department,或者您需要一个表来将Department连接到Course。在我的例子中,我采取了简单的方法,并将部门添加到EnrollCourse

DECLARE @enrollCourse TABLE (
ID int,
StudentID int,
Course int,
Department int
)

DECLARE @studentGrade TABLE (
ID int,
Department int,
StudentID int,
Grade varchar(2),
Course int
)

INSERT INTO @enrollCourse VALUES (1, 2002, 102, 2)
INSERT INTO @enrollCourse VALUES (2, 2002, 104, 2)

SELECT ec.ID, ec.Department, ec.StudentID, COALESCE(sG.Grade, 'Not Graded Yet') as Grade, eC.Course FROM @enrollCourse eC left join 
(SELECT ID, Department, StudentID, Grade, Course FROM @studentGrade s INNER JOIN (SELECT MAX(ID) as MaxID FROM @studentGrade GROUP BY Department, StudentID, Course) mG ON s.ID = mG.MaxID)  sG ON
ec.StudentID = sG.StudentID and ec.Course = sg.Course and ec.Department = sg.Department 

INSERT INTO @studentGrade VALUES(1, 2, 2002, 'B+', 102) 

SELECT ec.ID, ec.Department, ec.StudentID, COALESCE(sG.Grade, 'Not Graded Yet') as Grade, eC.Course FROM @enrollCourse eC left join 
(SELECT ID, Department, StudentID, Grade, Course FROM @studentGrade s INNER JOIN (SELECT MAX(ID) as MaxID FROM @studentGrade GROUP BY Department, StudentID, Course) mG ON s.ID = mG.MaxID)  sG ON
ec.StudentID = sG.StudentID and ec.Course = sg.Course and ec.Department = sg.Department 

DELETE FROM @studentGrade

INSERT INTO @studentGrade VALUES(1, 2, 2002, 'A+', 104) 

SELECT ec.ID, ec.Department, ec.StudentID, COALESCE(sG.Grade, 'Not Graded Yet') as Grade, eC.Course FROM @enrollCourse eC left join 
(SELECT ID, Department, StudentID, Grade, Course FROM @studentGrade s INNER JOIN (SELECT MAX(ID) as MaxID FROM @studentGrade GROUP BY Department, StudentID, Course) mG ON s.ID = mG.MaxID)  sG ON
ec.StudentID = sG.StudentID and ec.Course = sg.Course and ec.Department = sg.Department 

INSERT INTO @studentGrade VALUES(2, 2, 2002, 'B+', 102) 

SELECT ec.ID, ec.Department, ec.StudentID, COALESCE(sG.Grade, 'Not Graded Yet') as Grade, eC.Course FROM @enrollCourse eC left join 
(SELECT ID, Department, StudentID, Grade, Course FROM @studentGrade s INNER JOIN (SELECT MAX(ID) as MaxID FROM @studentGrade GROUP BY Department, StudentID, Course) mG ON s.ID = mG.MaxID)  sG ON
ec.StudentID = sG.StudentID and ec.Course = sg.Course and ec.Department = sg.Department 

INSERT INTO @studentGrade VALUES(3, 2, 2002, 'B+', 104) 

SELECT ec.ID, ec.Department, ec.StudentID, COALESCE(sG.Grade, 'Not Graded Yet') as Grade, eC.Course FROM @enrollCourse eC left join 
(SELECT ID, Department, StudentID, Grade, Course FROM @studentGrade s INNER JOIN (SELECT MAX(ID) as MaxID FROM @studentGrade GROUP BY Department, StudentID, Course) mG ON s.ID = mG.MaxID)  sG ON
ec.StudentID = sG.StudentID and ec.Course = sg.Course and ec.Department = sg.Department