1

我有以下查询给出了req_no and order_noorder_no是一个 sub_query ,您可以从下面的 sql 中看到。对于少数req_no人来说,不止一个order_no's,因此我得到了ORA-01427: single-row subquery returns more than one row.

我想同时显示order_nofor one req_no,我该如何实现呢?任何帮助都是非常可观的。

谢谢

PS我们客户的一个数据库仍然是Oracle 8i

SELECT  max_qst.req_no,
            (SELECT  DISTINCT max_odr.order_no
                FROM     maximo_orders max_odr,
                         maximo_order_revisions max_odv,
                         maximo_order_items max_odi,
                         maximo_order_dates max_odd,
                         maximo_requisition_order max_rqo,
                         maximo_requisition_details max_req
              WHERE       max_req.req_no = max_qst.req_no
                         AND max_req.req_yr = max_qst.req_yr
                         AND max_odr.order_no = max_odi.order_no
                         AND max_odi.order_item_id = max_odd.order_item_id
                         AND max_req.requisition_item_id = max_rqo.requisition_item_id
                         AND max_rqo.order_schedule_id = max_odd.order_schedule_id
                         AND max_odv.order_no = max_odi.order_no
                         AND max_odv.revision_no =
                                 (SELECT   MAX (max_alias.revision_no)
                                     FROM   maximo_order_revisions max_alias
                                    WHERE   max_alias.order_no = max_odv.order_no)
                         AND maximo_order_item (max_odi.order_no,
                                                                  max_odv.revision_no,
                                                                  max_odi.order_item_id
                                                                 ) = 'CONFIRMED'
                        )
  FROM  maximo_requisitions max_qst, maximo_requisition_details max_qsd
 WHERE       max_qst.qst_id = max_qsd.qst_id
            AND max_qst.enter_date = '2001'
            AND max_qst.req_no = 'PUR_12WX'

Update 1

想要输出。

REQ_No     ORDER_NO

PUR_12WX   PR_9078  
PUR_12WX   PR_9079  
4

2 回答 2

2

使用连接而不是相关子查询。

我已经max_qst从子查询中删除了引用并将它们移动到连接谓词中。

我也刚刚将其更改为使用LEFT JOIN. 这允许没有order_no返回值的可能性。

SELECT
  max_qst.req_no,
  sub_query.order_no
FROM
  maximo_requisitions          max_qst
INNER JOIN
  maximo_requisition_details   max_qsd
    ON max_qst.qst_id = max_qsd.qst_id
LEFT JOIN
(
  SELECT DISTINCT
    max_odr.order_no,
    max_req.req_no,
    max_req.req_yr
  FROM
    maximo_orders              max_odr,
    maximo_order_revisions     max_odv,
    maximo_order_items         max_odi,
    maximo_order_dates         max_odd,
    maximo_requisition_order   max_rqo,
    maximo_requisition_details max_req
  WHERE
        max_odr.order_no            = max_odi.order_no
    AND max_odi.order_item_id       = max_odd.order_item_id
    AND max_req.requisition_item_id = max_rqo.requisition_item_id
    AND max_rqo.order_schedule_id   = max_odd.order_schedule_id
    AND max_odv.order_no            = max_odi.order_no
    AND max_odv.revision_no         = (SELECT MAX (max_alias.revision_no)
                                         FROM maximo_order_revisions max_alias
                                        WHERE max_alias.order_no = max_odv.order_no)
    AND maximo_order_item (max_odi.order_no,  max_odv.revision_no, max_odi.order_item_id) = 'CONFIRMED'
)
  suq_query
    ON  max_qst.req_no = sub_query.req_no
    AND max_qst.req_yr = sub_query.req_yr
WHERE
  max_qst.enter_date = '2001'
  max_qst.req_no = 'PUR_12WX'
于 2012-08-08T08:07:39.027 回答
0

您可以使用

DISTINCT 

或者

WHERE ROWNUM = 1

但我建议您调查一下为什么返回的数据不止一行:您是否误解了数据、是否错过了连接、是否存在错误或重复的数据等。

于 2012-08-08T07:54:42.703 回答