获取在 Oracle 中执行操作的学生列表

Kyl*_*cot 1 oracle

我有学生动作的表(idstudent_idtype,和quarter)。我需要获取所有学生的列表,以及他们在当前季度和上一季度是否有特定操作的标志,但我不确定如何在 Oracle SQL 中执行此操作。

student_actions 表

在此处输入图片说明

假设当前季度是 3,并且我只对ExpulsionProbation操作感兴趣,我将如何获得像下面模拟的结果集?

预期成绩

在此处输入图片说明

Col*_*art 5

这只是一个聚合问题。

select
  student_id,
  count(case when type in ('Expulsion','Probation') and quarter=3-1 then 1 end) as previous,
  count(case when type in ('Expulsion','Probation') and quarter=3 then 1 end) as "CURRENT"
from student_actions
group by student_id;
Run Code Online (Sandbox Code Playgroud)

http://sqlfiddle.com/#!4/f4263/10/0