子查询返回多个值

时间:2012-08-08 07:45:36

标签: sql database oracle oracle8i

我有以下查询,它给出了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_no的{​​{1}},我该如何实现? 任何帮助都非常值得赞赏。

由于

P.S。我们客户的一个数据库仍为req_no

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

2 个答案:

答案 0 :(得分: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'

答案 1 :(得分:0)

您可以使用

DISTINCT 

WHERE ROWNUM = 1

但我建议您调查为什么您返回了多行:您是否误解了数据,错过了连接,是否存在错误或重复的数据等等。 / p>