3个查询的1个结果集?

时间:2013-10-22 17:27:46

标签: oracle join

我最终试图让这件事发生。目前我有3个单独的查询。出于自动化目的,我需要在一个输出中获得这三个查询的结果,我似乎无法正确地加入它们以获得预期的结果。

QUERY 1:

SELECT OH.EXTN_HOST_ORDER_REF,
       OL.EXTN_HOST_ORDER_LINE_REF,
       OL.ORIGINAL_ORDERED_QTY,
       OL.EXTN_TENDER_QUANTITY,
       OL.EXTN_CUM_PICK_QTY,
       OL.SHIPPED_QUANTITY,
       OL.EXTN_REFUND_QTY
FROM   YFS_ORDER_HEADER OH,
       YFS_ORDER_LINE OL
WHERE  OH.ORDER_HEADER_KEY = OL.ORDER_HEADER_KEY
       AND OH.DOCUMENT_TYPE = '0001'
       AND OH.EXTN_HOST_ORDER_REF = 'xxxxxxxxxxx'
ORDER  BY PL.EXTN_HOST_ORDER_LINE_REF ASC;

QUERY 2:

SELECT RS.STATUS_QUANTITY AS RETURNED_QTY
FROM   YFS_ORDER_HEADER OH,
       YFS_ORDER_LINE OL,
       YFS_ORDER_RELEASE_STATUS RS
WHERE  OH.ORDER_HEADER_KEY = OL.ORDER_HEADER_KEY
       AND OL.ORDER_LINE_KEY = RS.ORDER_LINE_KEY
       AND RS.STATUS = '3700.02'
       AND OH.EXTN_HOST_ORDER_REF = 'xxxxxxxxxxx';

QUERY 3

SELECT RS.STATUS_QUANTITY AS CANCELLED_QTY
FROM   YFS_ORDER_HEADER OH,
       YFS_ORDER_LINE OL,
       YFS_ORDER_RELEASE_STATUS RS
WHERE  OH.ORDER_HEADER_KEY = OL.ORDER_HEADER_KEY
       AND OL.ORDER_LINE_KEY = RS.ORDER_LINE_KEY
       AND RS.STATUS = '9000'
       AND OH.EXTN_HOST_ORDER_REF = 'xxxxxxxxxxx'; 

查询应显示NULL值,其中查询2& 3.

提前感谢您的帮助和建议!

1 个答案:

答案 0 :(得分:0)

如果您允许显示已退回和已取消的数量,则以下简单编辑应该有效。希望这会有所帮助。

  SELECT oh.extn_host_order_ref,
         ol.extn_host_order_line_ref,
         ol.original_ordered_qty,
         ol.extn_tender_quantity,
         ol.extn_cum_pick_qty,
         ol.shipped_quantity,
         ol.extn_refund_qty,
         DECODE (rs.status, '3700.02', rs.status_quantity) AS returned_qty,
         DECODE (rs.status, '9000', rs.status_quantity) AS cancelled_qty
    FROM yfs_order_header oh
         INNER JOIN yfs_order_line ol
            ON oh.order_header_key = ol.order_header_key
         LEFT OUTER JOIN yfs_order_release_status rs
            ON ol.order_line_key = rs.order_line_key
   WHERE oh.document_type = '0001' AND oh.extn_host_order_ref = 'xxxxxxxxxxx'
ORDER BY pl.extn_host_order_line_ref ASC;