その問題は、あなたがにのみ参加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 |