我有一个评估结果数据库,其中包含确定学生、年级、年份和学期的字段。下面的查询找到了在某个起点和终点对特定主题进行评估的学生:
SELECT
a1.PupilId
FROM assessment as a1
JOIN assessment as a2
USING (PupilId)
WHERE a1.GradeID in('1A','1B','1C')
AND a2.GradeID in('5A','5B','5C')
AND a1.NCYear = '1'
AND a1.Term = '6'
AND a2.NCYear = '2'
AND a2.Term = '6'
AND a1.Subject = '31'
AND a2.Subject = '31'
AND a1.Type = 'assessment'
AND a2.Type = 'assessment'
这很好用,但现在我想找到具有起始年级(即 NCYear='1' 和 Term='6')但没有最终成绩条目的学生。我玩过NOT EXISTS,但无法让它工作。
为了澄清,这是评估表:
id | Subject | PupilId | NCYear | Term | GradeID | Type
--------------------------------------------------------
1 | 31 | 1 | 1 | 6 | 1A | assessment
2 | 31 | 1 | 2 | 6 | 5A | assessment
3 | 31 | 2 | 1 | 6 | 1A | assessment
上面的 SQL 查询应该返回 PupilID 1。我需要返回 PupilID 2 的查询,开始评估在那里但没有结束评估。
有什么建议么?