要使用的SQL代码不存在

时间:2013-08-30 17:07:22

标签: sql oracle oracle11g

Per_all_people_f

name  person_id  emp_flag  effective_start_date   effective_end_date   DOJ
--------------------------------------------------------------------------------    
ABC   123          Y       30-MAR-2011              30-MAR-2013       10-FEB-2011
ABC   123          Y       24-FEB-2011              27-FEB-2011       10-FEB-2011
DEF   345          N       10-APR-2012              30-DEC-4712       15-SEP-2011

per_chcklist

 person_id   name
  ------------------   
123         C1
456         C2 
345         C3 

ACTUAL_TERMINATION_DATE

person_id   TERM_DATE
      ------------------   
    123         30-SEP-2013
    456         30-AUG-2013
    345         29-AUG-2013

现在我想要计算所有那些在Per_all_people_f中的人ID,其中TERM_DATE在ACTUAL_TERMINATION_DATE中,但不在per_chcklist中。

我想在查询中使用简单不存在。我写的查询是: -

select papf.person_id,
papf.name
from per_all_people_f papf,
per_chcklist pc,
ACTUAL_TERMINATION_DATE atd

where papf.person_id=atd.person_id
and atd.TERM_DATE is not null
and pc.person_is!=papf.person_id;

但这并没有给我答案。

1 个答案:

答案 0 :(得分:0)

我可能会建议您使用existsnot exists来撰写此查询。它更符合您的要求:

select papf.person_id, papf.name
from per_all_people_f papf
where exists (select 1
              from ACTUAL_TERMINATION_DATE atd
              where atd.TERM_DATE = papf.TERM_DATE and atd.person_id = papf.person_id
             ) and
      not exists (select 1
                  from per_chcklist pc
                  where papf.person_id = pc.person_id
                 );

完成这项工作后,如果您想了解有关连接的更多信息,可以弄清楚如何将其表达为显式连接。