我要去猜测,这个问题是,你只person
加入orgstructure
到coursescompleted
,我想你也需要加入培训:
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 with Demo
结果是:
| 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 |
如何查询变得'不准确,invalid'?请解释。 –
嗨马哈茂德,感谢你的时间。 bluefeet将它钉在下面,第二个条件不适合,因此加入的关系不准确。 – Smudger