1
SELECT b.KPCNO
         ,b.KPC_FULL_NAME    

FROM xxkpc_hr_personnel_v2  b
WHERE  b.type(+) = 'KPC Employee'
       AND b.DESIGNATION is null
       AND b.kpcno IS NOT NULL
       AND b.ORGANIZATION_ID=(select g.ORGANIZATION_ID from  xxkpc_fn_web_personnel_v g where g.kpcno = :kpcno)
GROUP BY b.KPCNO
          ,b.kpc_full_name

UNION

SELECT b.KPCNO
         ,b.KPC_FULL_NAME
FROM xxkpc_hr_pos_struct_code_comb   a
     ,xxkpc_hr_personnel_v2  b
WHERE b.position_id(+) = a.child_position_id
       AND b.type(+) = 'KPC Employee'
       AND b.kpcno IS NOT NULL
       AND a.PERANT_POSITION_ID =  (select g.POSITION_ID from  xxkpc_fn_web_personnel_v g where g.kpcno = :kpcno)
GROUP BY b.KPCNO
          ,b.kpc_full_name

请我需要将这 2 个查询合并为一个查询而不使用 UNION

4

3 回答 3

2

我想你可以这样写:

select distinct b.kpcno, b.kpc_full_name
  from xxxkpc_hr_personnel_v2 b
 where b.type(+) = 'KPC Employee'
   and b.kpcno is not null
   and (
         (b.designation is null and
          b.organization_id in (select g.organization_id
                                  from xxkpc_fn_web_personnel_v g 
                                 where g.kpcno = :kpcno))
         or
        (b.position_id in (select a.child_position_id
                             from xxkpc_hr_pos_struct_code_comb a
                            where a.parent_position_id = (select g.position_id
                                                            from xxkpc_fn_web_personnel_v g 
                                                           where g.kpcno = :kpcno)))
       )

据我所知,您正在使用group by来制作结果distinct,所以我使用了正确的关键字。And aandb没有左连接,因为在下一个 where anded 子句中你说的是a.parent_position_id = ....

于 2012-06-05T18:03:18.893 回答
1
SELECT 
    b.KPCNO,
    b.KPC_FULL_NAME
FROM 
    xxkpc_hr_personnel_v2 b
        LEFT OUTER JOIN xxkpc_hr_pos_struct_code_comb a ON b.position_id = a.child_position_id
        INNER JOIN xxkpc_fn_web_personnel_v g ON g.kpcno = :kpcno
WHERE  
    b.type = 'KPC Employee'
AND b.kpcno IS NOT NULL
AND ((b.DESIGNATION IS NULL AND g.ORGANIZATION_ID = b.ORGANIZATION_ID) 
    OR (a.PERANT_POSITION_ID = g.POSITION_ID))
于 2012-06-05T18:03:25.267 回答
0

你有没有加入这两个表?看起来你可以加入 b.KPCNO 或 b.KPC_FULL_NAME。

于 2012-06-05T17:53:45.067 回答