SQL层次结构查询

时间:2012-07-29 08:01:56

标签: sql database oracle11g

SELECT ppse.SUBORDINATE_POSITION_ID child_position_id
               ,ppse.PARENT_POSITION_ID manger_position_id
               ,b.person_id
FROM per_pos_structure_elements_v ppse
      ,APPS.xxkpc_hr_personnel_v2_mv  b
WHERE b.position_id(+) = ppse.SUBORDINATE_POSITION_ID

AND ppse.POS_STRUCTURE_VERSION_ID =64
AND ppse.PARENT_POSITION_ID=12493

此查询的输出:

12604   12493   400 
12605   12493   108 
13644   12493   37897   
12752   12493   18  
13643   12493       
13642   12493       
13641   12493       



SELECT ppse.SUBORDINATE_POSITION_ID child_position_id
               ,ppse.PARENT_POSITION_ID manger_position_id
               ,b.person_id
FROM per_pos_structure_elements_v ppse
              ,APPS.xxkpc_hr_personnel_v2_mv  b
WHERE b.position_id(+) = ppse.SUBORDINATE_POSITION_ID

AND ppse.POS_STRUCTURE_VERSION_ID =64
AND ppse.PARENT_POSITION_ID=12504


12514   12504   449
12483   12504   450
12487   12504   456
12497   12504   156
12889   12504   168
12493   12504   

12493是第一个查询中的父项,并且是第二个查询中的子项,因此我需要删除所有具有null person_id(第三列)的行。不是第二个查询中的子项的父项我不能删除最后一行因为他是第一个查询中的子项的父项,但是要删除第一个查询中的最后3行

1 个答案:

答案 0 :(得分:0)

尝试这样的事情......

SELECT ppse.SUBORDINATE_POSITION_ID child_position_id
           ,ppse.PARENT_POSITION_ID manger_position_id
           ,b.person_id
FROM per_pos_structure_elements_v ppse
  ,APPS.xxkpc_hr_personnel_v2_mv  b
WHERE b.position_id(+) = ppse.SUBORDINATE_POSITION_ID
AND b.type(+) = 'KPC Employee'
AND ppse.POS_STRUCTURE_VERSION_ID =64
AND ppse.PARENT_POSITION_ID=12493
AND 
(b.person_id!=null OR 
 ppse.SUBORDINATE_POSITION_ID in
             (SELECT SUBORDINATE_POSITION_ID from per_pos_structure_elements_v))