显示谁重考并通过了考试,课程是什么?

show who resit and passed and what course was it?

我需要帮助找出谁在考试中失败并重考并通过考试,

代码如下:

select STUDENT_ID,EXAM_ID,SCORE,PASS_THRESHOLD,s.NAME , c.NAME as Course_name, EXAM_DT, 
case 
 when SCORE>=PASS_THRESHOLD then 'PASS'
 else 'Fail'
end as Flag
from exam_submission es 
left join student s  on es.STUDENT_ID  = s.ID
left join exam e    on es.EXAM_ID  = e.ID 
left join course c  on e.COURSE_ID  = c.ID

结果如下:

STUDENT_ID  EXAM_ID SCORE   PASS_THRESHOLD  NAME    Course_name     EXAM_DT      Flag
    1          3      88    65           Anthony    Data Mining     2019-12-17   PASS
    1          5      71    70           Anthony    Statistic       2019-12-19   PASS
    2          1      53    55           Sisca      Machine Learning2019-12-17  Fail
    2          3      77    65           Sisca      Data Mining     2019-12-17  PASS
    2          4      85    63           Sisca      Data Science    2019-12-18  PASS
    2          1      60    55           Sisca      Machine Learning2020-01-08  PASS

我需要这样查找:

 2         1      53    55           Sisca      Machine Learning2019-12-17  Fail
 2         1      60    55           Sisca      Machine Learning2020-01-08  PASS

可能使用如下查询。 这是使用您的查询作为输入。 此外,我们假设不可能让一个学生按时间顺序在两年的同一次考试中获得(通过,失败)。

; with inputdata as 
(
 select STUDENT_ID,EXAM_ID,SCORE,PASS_THRESHOLD,s.NAME , c.NAME as Course_name, EXAM_DT, 
    case 
     when SCORE>=PASS_THRESHOLD then 'PASS'
     else 'Fail'
    end as Flag
    from exam_submission es 
    left join student s  on es.STUDENT_ID  = s.ID
    left join exam e    on es.EXAM_ID  = e.ID 
    left join course c  on e.COURSE_ID  = c.ID
)
    
 select * from Inputdata I
join 
 ( select student_id, exam_id from 
    inputdata 
    group by student_id, exam_id
    having count(distinct flag)=2
)T on I.student_id=T.student_id and I.exam_id=T.exam_id
order by exam_dt asc