oracle查询中有多个可选条件

时间:2016-10-18 10:17:41

标签: sql oracle

我在表格中列出数据。

AP_ID     | AP_VERSION_ID | ORDER_ACTION_ID | AP_ID_PARENT | MAIN_IND
----------+---------------+-----------------+--------------+---------
100000002 | 0             |                 |              | 0
100000003 | 0             | 5               | 100000002    | 1
100000012 | 0             | 12              | 100000010    | 0
100000013 | 0             | 12              | 100000012    | 0

我希望ap_id和ap_id父级具有beloow查询

select AI.AP_ID || AI.AP_VERSION_ID,
   DOM.AP_ID || DOM.AP_VERSION_ID,
   AI.ORDER_ACTION_ID,
   DOM.ORDER_ACTION_ID,
   ai.main_ind
   from omsrepdb11.tbap_item ai ,omsrepdb11.tbap_item dom  
   where AI.AP_ID_Parent = DOM.AP_ID (+)
  AND AI.ORDER_ACTION_ID = DOM.ORDER_ACTION_ID(+)
   and ai.ap_id in (100000003,100000013);

但它没有提供所需的数据。对于ap_id 100000003,它给出了空的ap_id Parent,如下所示。

AP_ID_VERSION | DOM_ID_VERSION | AI_OA_ID | DOM_OA_ID | MAIN_IND
--------------+----------------+----------+-----------+---------
1000000030    |                | 5        |           | 1
1000000130    | 1000000120     | 12       | 12        | 0

3 个答案:

答案 0 :(得分:1)

您的问题是您的表中没有AP_ID = 100000002和ORDER_ACTION_ID = 5的条目,因此100000003记录没有父匹配。因此dom.ap_iddom.ap_version_id为空,因为您已加入此记录。所以你得到的结果是正确的。

要将1000000020作为1000000030的父级,您可以使用ai.ap_id_parent代替dom.ap_id,并决定如何获取缺失的dom.ap_version_id。 E.g:

select 
  ai.ap_id || ai.ap_version_id,
  ai.ap_id_parent || coalesce(dom.ap_version_id, ai.ap_version_id),
  ai.order_action_id,
  dom.order_action_id,
  ai.main_ind
from omsrepdb11.tbap_item ai 
left join omsrepdb11.tbap_item dom on  dom.ap_id = ai.ap_id_parent
                                   and dom.order_action_id = ai.order_action_id
where ai.ap_id in (100000003,100000013);

但如上所述,您的结果是正确的。所以想想你真正想要的东西。也许你根本不想加入id + order_action_id。

以下是仅在order_action_id不为1时加入main_ind的查询。

select 
  ai.ap_id || ai.ap_version_id,
  dom.ap_id || dom.ap_version_id,
  ai.order_action_id,
  dom.order_action_id,
  ai.main_ind
from omsrepdb11.tbap_item ai 
left join omsrepdb11.tbap_item dom 
            on  dom.ap_id = ai.ap_id_parent
            and (ai.main_ind = 1 or dom.order_action_id = ai.order_action_id)
where ai.ap_id in (100000003,100000013);

答案 1 :(得分:0)

首先使用正确的显式join语法:

select . . .
from omsrepdb11.tbap_item ai left join
     omsrepdb11.tbap_item dom  
     on ai.AP_ID_Parent = dom.AP_ID and
        ai.ORDER_ACTION_ID = dom.ORDER_ACTION_ID
where ai.ap_id in (100000003, 100000013);

答案 2 :(得分:0)

问题在于您的加入条件并未考虑dom.order_action_id为空的时间。

我认为你是在以下之后做的:

WITH tbap_item AS (SELECT 100000002 ap_id, 1 ap_version_id, NULL order_action_id, NULL ap_id_parent, 0 main_ind FROM dual UNION ALL
                   SELECT 100000003 ap_id, 0 ap_version_id, 5 order_action_id, 100000002 ap_id_parent, 1 main_ind FROM dual UNION ALL
                   SELECT 100000012 ap_id, 0 ap_version_id, 12 order_action_id, 100000010 ap_id_parent, 0 main_ind FROM dual UNION ALL
                   SELECT 100000013 ap_id, 0 ap_version_id, 12 order_action_id, 100000012 ap_id_parent, 0 main_ind FROM dual)
SELECT ai.ap_id || ai.ap_version_id ap_id_version,
       dom.ap_id || dom.ap_version_id dom_id_version,
       ai.order_action_id ai_oa_id,
       dom.order_action_id dom_oa_id,
       ai.main_ind
FROM   tbap_item ai
       LEFT OUTER JOIN tbap_item dom ON ai.ap_id_parent = dom.ap_id
                                        AND ai.order_action_id = NVL(dom.order_action_id, ai.order_action_id)
WHERE  ai.ap_id IN (100000003, 100000013);

AP_ID_VERSION DOM_ID_VERSION   AI_OA_ID  DOM_OA_ID   MAIN_IND
------------- -------------- ---------- ---------- ----------
1000000030    1000000021              5                     1
1000000130    1000000120             12         12          0