我有学生动作的表(id
,student_id
,type
,和quarter
)。我需要获取所有学生的列表,以及他们在当前季度和上一季度是否有特定操作的标志,但我不确定如何在 Oracle SQL 中执行此操作。
假设当前季度是 3,并且我只对Expulsion
和Probation
操作感兴趣,我将如何获得像下面模拟的结果集?
这只是一个聚合问题。
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