我有两个表,一个表将用户标记为已通过课程,还有一个表每个作业代码的课程列表。我正在尝试查询以返回所有缺少类的用户的记录。
以下是表格:
Attended
--------
empid jobcode classcode grade
555 1 100 A
555 1 101 A
444 2 200 A
JobClassCode
--------
jobcode classcode
1 100
1 101
1 102
2 100
2 200
3 300
3 301
我从此查询开始,以查找缺少用户的类:
select * from attended at
right outer join jobcodeclass jc on at.jobcode = jc.jobcode and at.classcode = jc.classcode
然后我尝试使用它来构建相关的子查询,但是我看不到同时返回用户ID和缺少的课程ID的方法:
select * from jobcodeclass oq where classcode in (select jc.classcode from attended at
right outer join jobcodeclass jc on at.jobcode = jc.jobcode
and at.classcode = jc.classcode and jc.jobcode = oq.jobcode
and oq.classcode = jc.classcode and empid is null)
答案 0 :(得分:1)
通过加入jobcode
来生成每个员工所需的所有可能的类。看看学生参加了哪些课程:
select ej.empid, ej.jobcode, jss.classcode
from (select distinct empid, jobcode from attended) ej join
JobClassCode jcc
on jcc.jobcode = ej.jobcode left join
attended a
on a.empid= e.empid and a.jobcode = ej.jobcode and
a.classcode = jcc.classcode
where a.empid is null;
如果您只需要员工,请使用select distinct ej.empid
。