2013-08-30 53 views
0

不存在Per_all_people_fSQL代碼使用

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 

現在我想計算所有所有這些人的ID這是在Per_all_people_f和其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; 

但這並沒有給我答案。

+0

您應該用標準顯式連接替換隱式連接。也就是說:'FROM table1 a JOIN table2 b ON a.ID = b.ID JOIN table3 c ON a.ID = c.ID'等 –

回答

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 
       ); 

後,你有這樣的工作,你能弄清楚如何表達這種明確連接,如果你想了解更多關於加入。