我有一个场景 - 用户需要完成2组3门课程中的任何一门,然后才能通过。如果他只完成1或2门课程,结果将为0.如果他完成所有课程,则返回结果为1.
必须通过CourseA,CourseB,CourseC才能通过 或完成CourseD,CourseE,CourseF传递
答案 0 :(得分:0)
如果基表有三列,这将是一个很好的练习题:student_id,course_id和grade(通过/失败),还有另一个课程表,包含course_id和“track”(可能还有其他关于课程的信息) )。 “轨道”是指必须通过才能毕业的一组课程;例如,一个“轨道”需要课程A,B,C(假设那些是课程ID),另一个需要D,E,F。
这是一个原型,可能的解决方案。
with
enrollments ( student_id, course_id, grade ) as (
select 1001, 'A', 'Pass' from dual union all
select 1001, 'B', 'Pass' from dual union all
select 1001, 'D', 'Fail' from dual union all
select 1001, 'E', 'Pass' from dual union all
select 1002, 'A', 'Pass' from dual union all
select 1002, 'C', 'Pass' from dual union all
select 1002, 'D', 'Pass' from dual union all
select 1003, 'F', 'Fail' from dual union all
select 1005, 'A', 'Fail' from dual union all
select 1005, 'D', 'Pass' from dual union all
select 1005, 'E', 'Pass' from dual union all
select 1005, 'F', 'Pass' from dual
),
courses ( course_id, track ) as (
select 'A', 101 from dual union all
select 'B', 101 from dual union all
select 'C', 101 from dual union all
select 'D', 201 from dual union all
select 'E', 201 from dual union all
select 'F', 201 from dual
)
-- >> end of test data; the actual solution (SQL query) begins below this line << --
select distinct student_id
from enrollments e join courses c on e.course_id = c.course_id
where grade = 'Pass'
group by student_id, track
having count(*) = 3
;
STUDENT_ID
----------
1005