Oracle不存在

时间:2015-12-01 16:42:17

标签: oracle

大家好我已经写了一个查询,根据类的完成返回一些结果。我在编写基于相同类的异常报告时遇到问题。我想我需要使用" NOT EXISTS"子查询但不确定如何。

以下是完成查询

select papf.employee_number
 , papf.full_name
 , haou.name
 , oe.title
 , oe.course_end_date completed_date
 , oav.version_name
from per_all_people_f papf
 , per_all_assignments_f paaf
 , hr_all_organization_units haou
 , ota_events oe
 , ota_delegate_bookings odb
 , ota_activity_versions oav
where papf.person_id = paaf.person_id
and paaf.organization_id = haou.organization_id
and odb.event_id = oe.event_id
and odb.booking_status_type_id in (1006, 1004)
and odb.delegate_person_id = paaf.person_id
and oe.activity_version_id in (3, 1001, 2, 8, 2001)
and oe.activity_version_id = oav.activity_version_id
and haou.organization_id = 4729
and trunc(sysdate) between papf.effective_start_date and papf.effective_end_date
and trunc(sysdate) between paaf.effective_start_date and paaf.effective_end_date
order by papf.employee_number;

我想为尚未完成这些课程的员工返回papf.employee_number,papf.full_name和oav.version_name的结果。 所以oe.activity_version_id不存在(3,1001,1,8,2001) 我有点困惑,我应该在哪里这样做?

谢谢

2 个答案:

答案 0 :(得分:0)

尝试:

select papf.employee_number
 , papf.full_name
 , haou.name
 , oe.title
 , oe.course_end_date completed_date
 , oav.version_name
from per_all_people_f papf
 , per_all_assignments_f paaf
 , hr_all_organization_units haou
 , ota_events oe
 , ota_delegate_bookings odb
 , ota_activity_versions oav
where papf.person_id = paaf.person_id
and paaf.organization_id = haou.organization_id
and odb.event_id = oe.event_id
and odb.booking_status_type_id in (1006, 1004)
and odb.delegate_person_id = paaf.person_id
/* and oe.activity_version_id in (3, 1001, 2, 8, 2001) */
and NOT EXISTS( 
   SELECT null FROM  ota_events oe1
   WHERE oe1.activity_version_id = oav.activity_version_id
     AND odb.event_id = oe1.event_id
     AND oe1.activity_version_id in (3, 1001, 2, 8, 2001)
)
and oe.activity_version_id = oav.activity_version_id
and haou.organization_id = 4729
and trunc(sysdate) between papf.effective_start_date and papf.effective_end_date
and trunc(sysdate) between paaf.effective_start_date and paaf.effective_end_date
order by papf.employee_number;

答案 1 :(得分:0)

根据您提到的内容(以及用户 symcbean 指出,为了避免与初始SQL中的逻辑相矛盾),您可能需要一个单独的查询,如下所示,以获得结果。尝试使用NOT IN。

select papf.employee_number
 , papf.full_name
 , haou.name
 , oe.title
 , oe.course_end_date completed_date
 , oav.version_name
from per_all_people_f papf
 , per_all_assignments_f paaf
 , hr_all_organization_units haou
 , ota_events oe
 , ota_delegate_bookings odb
 , ota_activity_versions oav
where papf.person_id = paaf.person_id
and paaf.organization_id = haou.organization_id
and odb.event_id = oe.event_id
and odb.booking_status_type_id in (1006, 1004)
and odb.delegate_person_id = paaf.person_id
and oe.activity_version_id NOT IN (3, 1001, 2, 8, 2001)
and oe.activity_version_id = oav.activity_version_id
and haou.organization_id = 4729
and trunc(sysdate) between papf.effective_start_date and papf.effective_end_date
and trunc(sysdate) between paaf.effective_start_date and paaf.effective_end_date
order by papf.employee_number;

HTH