我猜这个问题是你只加入了orgstructure
,coursescompleted
我person
认为你还需要参加培训:
select o.person,
o.job,
j.risk,
r.training,
c.course,
c.person,
c.datecompleted
from orgstructure o
left outer join jobsrisks j
on o.job=j.job
left outer join risktraining r
on j.risk=r.risk
left outer join coursescompleted c
on o.person=c.person
and r.training = c.course --- add this
where o.department='finance'
如果该人已完成与工作相关的每种风险的课程,则您需要加入该人。
将整个查询放在一起,您将拥有:
select o.person,
o.job,
j.risk,
r.training,
case when c.course is null then 'no' else 'yes' end TrainingCompleted,
coalesce(c.datecompleted, 'n/a') datecompleted
from orgstructure o
left outer join jobsrisks j
on o.job=j.job
left outer join risktraining r
on j.risk=r.risk
left outer join coursescompleted c
on o.person=c.person
and r.training = c.course
where o.department='finance'
请参阅带有演示的 SQL Fiddle
结果是:
| PERSON | JOB | RISK | TRAINING | TRAININGCOMPLETED | DATECOMPLETED |
-------------------------------------------------------------------------------------------------------------------------------
| taylor chetty | manager | safety | induction course | no | n/a |
| taylor chetty | manager | security | security course | no | n/a |
| bill thompson | data clerk | bad posture | personal wellbeing course | no | n/a |
| bill thompson | data clerk | repetitive strain injury | nursing course | yes | 2000-04-13 00:00:00 |
| bill thompson | data clerk | safety | induction course | yes | 2007-12-04 00:00:00 |
| ann brown | data clerk | bad posture | personal wellbeing course | no | n/a |
| ann brown | data clerk | repetitive strain injury | nursing course | no | n/a |
| ann brown | data clerk | safety | induction course | no | n/a |