使用Decode的SQL查询

时间:2013-04-25 16:32:28

标签: sql oracle oracle-sqldeveloper

您好我正在尝试创建一个查询,该查询将列出所有参加过该课程的学生在参加该课程之前没有完成该课程的所有先决条件。在采取后续行动之前必须先通过先决条件。注册的年份是学年。在学年内,秋季来临于春季之前的冬季。因此,由于学校系统,秋季来临之前。下面是我的数据库以及我的表格。我正在努力学习如何选择没有按正确顺序完成课程的学生。即在完成CID1之前完成CID2 这是我开始使用此代码

SELECT student.FirstName, student.Lastname, Student.SID FROM student
INNER JOIN enrolled ON student.SID = enrolled.StudentID
INNER JOIN prerequisite p ON enrolled.courseID = p.CID
INNER JOIN prerequisite p1 ON enrolled.courseID = p1.CID2
WHERE p1.CID1 = True AND p.CID = FALSE

1 个答案:

答案 0 :(得分:0)

SELECT  a, d, ea.studentId,
        ea.quarter AS a_quarter, ea.year AS a_year,
        ed.quarter AS d_quarter, ed.year AS d_year
FROM    (
        SELECT  cid1 a, cid2 d
        FROM    prerequisite
        CONNECT BY
                cid2 = PRIOR cid1
        ) c
JOIN    enrolled ea
ON      ea.courseId = c.a
LEFT JOIN
        enrolled ed
ON      ed.courseId = c.d
        AND ed.studentId = ea.studentId
WHERE   ed.studentId IS NULL
        OR
        ed.year > ed.year
        OR
        (ed.year = ea.year AND DECODE(ed.quarter, 'fall', 1, 'winter', 2, 'spring', 3) > DECODE(ea.quarter, 'fall', 1, 'winter', 2, 'spring', 3))