Oracle 我想添加带有日期参数的子查询以返回计算值,但我的查询不起作用

Oracle 我想添加带有日期参数的子查询以返回计算值,但我的查询不起作用,oracle,plsql,Oracle,Plsql,我有一个从oracle库存和采购中获取数据的查询。现在我想从po_line_locations_all添加一个子查询,并使用日期参数返回一个计算值,就像该值位于子查询show it else display zero中一样,但在我的例子中,如果子查询返回null,则查询将不显示任何内容 我想显示主查询的结果,即使子查询返回null SELECT distinct msib.segment1 Item_Code, MSIB.inventory_ite

我有一个从oracle库存和采购中获取数据的查询。现在我想从po_line_locations_all添加一个子查询,并使用日期参数返回一个计算值,就像该值位于子查询show it else display zero中一样,但在我的例子中,如果子查询返回null,则查询将不显示任何内容

我想显示主查询的结果,即使子查询返回null

SELECT distinct msib.segment1          Item_Code,
               MSIB.inventory_item_id,
               MSIB.organization_id   ORG_ID,

               msib.description             Item_Description,
               msib.primary_unit_of_measure UOM,
               ph.attribute1                Item_Type,
               SUM(plla.quantity) - SUM(plla.quantity_received) On_Order,
               ph.currency_code currency,
               max(pl.unit_price) FOB_Value_in_FCY,
               max(ph.rate_date),
               max(ph.rate) forex_rate,
               (
               SELECT  SUM (moq.transaction_quantity)
               FROM mtl_system_items_b msi, mtl_onhand_quantities moq
               WHERE moq.organization_id(+) = msi.organization_id
               AND moq.inventory_item_id(+) = msi.inventory_item_id
               and moq.ORGANIZATION_ID =MSIB.organization_id
               and msi.inventory_item_id = MSIB.inventory_item_id 
               GROUP BY msi.segment1, msi.organization_id
                ) CURR_STOCK,
               (
               SELECT NVL(ABS(sum(mtmt.transaction_quantity)),0) from MTL_MATERIAL_TRANSACTIONS mtmt 
               WHERE 1=1
               AND mtmt.inventory_item_id = MSIB.inventory_item_id --4018
               AND mtmt.organization_id = MSIB.organization_id--499
               and mtmt.TRANSACTION_ACTION_ID NOT IN (24, 30)
               AND to_date(mtmt.transaction_date) >= to_date(&Roll_back_date,'DD-MON-RRRR')
               )RB_TRANSACTIONS,
               (
               select ABS(SUM(mmt.transaction_quantity))
               from MTL_MATERIAL_TRANSACTIONS mmt
               where mmt.TRANSACTION_ACTION_ID NOT IN (24, 30)
               and (mmt.ORGANIZATION_ID = MSIB.organization_id --499--579
                   )
               and (mmt.INVENTORY_ITEM_ID = MSIB.inventory_item_id --4128 --4165 
                    and mmt.TRANSACTION_TYPE_ID in (33, 52)
                    )
               and (mmt.transaction_date between
                    to_date(add_months(&CONS_f_DATE, -12),'DD-MON-YYYY') 
                    AND to_date(&CONS_f_DATE, 'DD-MON-YYYY')
                    )
                AND (mmt.parent_transaction_id IS NULL)
                       ) annual_Consumption,
                (
                select ABS(SUM(mmt.transaction_quantity) / 4)
                FROM MTL_MATERIAL_TRANSACTIONS mmt
                WHERE mmt.TRANSACTION_ACTION_ID NOT IN (24, 30)
                and (mmt.ORGANIZATION_ID = MSIB.organization_id --499--579
                       )
                and (mmt.INVENTORY_ITEM_ID = MSIB.inventory_item_id --4128 --4165 
                AND mmt.TRANSACTION_TYPE_ID in (33, 52)
                )   
                and (mmt.transaction_date between
                                to_date(add_months(&CONS_f_DATE, -12),
                                         'DD-MON-YYYY') AND
                                to_date(&CONS_f_DATE, 'DD-MON-YYYY')) 
                   AND (mmt.parent_transaction_id IS NULL)
                ) months_Consumption,
                (
                select ABS((SUM(mmt.transaction_quantity) / 4) / 3)
                FROM MTL_MATERIAL_TRANSACTIONS mmt
                WHERE mmt.TRANSACTION_ACTION_ID NOT IN (24, 30)
                and (mmt.ORGANIZATION_ID = MSIB.organization_id --499--579
                       )
                and (mmt.INVENTORY_ITEM_ID = MSIB.inventory_item_id --4128 --4165 
                     AND mmt.TRANSACTION_TYPE_ID in (33, 52))
                   and (mmt.transaction_date between
                                to_date(add_months(&CONS_f_DATE, -12),
                                         'DD-MON-YYYY') AND
                                to_date(&CONS_f_DATE, 'DD-MON-YYYY')) 
                   AND (mmt.parent_transaction_id IS NULL)
                ) monthly_Average,
                 (
                 select MATERIAL_COST
                   FROM CST_ITEM_COST_TYPE_V vw
                  WHERE vw.organization_id = MSIB.organization_id
                    AND - 1 = -1
                    and (vw.INVENTORY_ITEM_ID = MSIB.inventory_item_id)
                    ) Unit_Cost, --new
                    sum(quan.t_quantity) - sum(r_quantity) finala


FROM mtl_system_items_b    MSIB, 
               PO_HEADERS_ALL        ph,
               Po_Lines_All          pl,
               PO_LINE_LOCATIONS_ALL PLLA,
-------------------SUBQUERY---------------------------------------
               (select nvl(sum(subplla.quantity),0) t_quantity, nvl(sum(subplla.quantity_received),0) r_quantity ,subpl.item_id

          from po_headers_all        subph,
               po_lines_all          subpl,
               po_line_locations_all subplla
         where subph.po_header_id = subpl.po_header_id
           and subplla.po_header_id = subph.po_header_id
           and subpl.po_line_id = subplla.po_line_id
           and subplla.org_id = subpl.org_id  
           and to_date(subplla.creation_date) >= to_date(&Roll_back_date,'DD-MON-RRRR')
         group by subph.attribute1, subph.currency_code, subpl.item_id       
         ) quan
-------------------SUBQUERY---------------------------------------


               WHERE 1=1
               and ph.po_header_id = pl.po_header_id
               and msib.inventory_item_id (+) = pl.item_id
               and pl.item_id (+) = quan.item_id
               and plla.po_header_id = ph.po_header_id
               and pl.po_line_id = plla.po_line_id
               and plla.org_id = pl.org_id

               and msib.organization_id in
                   (select haou.organization_id
                   from hr_organization_information hoi,
                   hr_all_organization_units   haou
                   where haou.organization_id = hoi.organization_id
                   and hoi.org_information1 = 'INV'
                   and hoi.org_information2 = 'Y'
                   and haou.name like '%HEIS%')

              and MSIB.Inventory_Item_Id=NVL(&ITEM,MSIB.Inventory_Item_Id)
              and MSIB.organization_id = nvl(&P_ORGI, MSIB.organization_id)
              AND to_date(plla.creation_date) BETWEEN
                  to_date(add_months(&Roll_back_date, -12),'DD-MON-YYYY') AND
                  to_date(&Roll_back_date,'DD-MON-YYYY')

GROUP BY  msib.segment1,
                  MSIB.inventory_item_id,
                  msib.description,
                  MSIB.organization_id,
                  msib.primary_unit_of_measure,
                  ph.attribute1, 
                  ph.currency_code

我的猜测是,您的问题只是使用旧的语法连接,而不是使用已经存在很长时间的东西

SELECT DISTINCT msib.segment1 Item_Code,
    MSIB.inventory_item_id,
    MSIB.organization_id ORG_ID,
    msib.description Item_Description,
    msib.primary_unit_of_measure UOM,
    ph.attribute1 Item_Type,
    SUM(plla.quantity) - SUM(plla.quantity_received) On_Order,
    ph.currency_code currency,
    max(pl.unit_price) FOB_Value_in_FCY,
    max(ph.rate_date),
    max(ph.rate) forex_rate,
    (
        SELECT SUM(moq.transaction_quantity)
        FROM mtl_system_items_b msi
        RIGHT JOIN mtl_onhand_quantities moq ON moq.organization_id = msi.organization_id
                                            AND moq.inventory_item_id = msi.inventory_item_id
        WHERE moq.ORGANIZATION_ID = MSIB.organization_id
            AND msi.inventory_item_id = MSIB.inventory_item_id
        GROUP BY msi.segment1,
            msi.organization_id
        ) CURR_STOCK,
    (
        SELECT NVL(ABS(sum(mtmt.transaction_quantity)), 0)
        FROM MTL_MATERIAL_TRANSACTIONS mtmt
        WHERE 1 = 1
            AND mtmt.inventory_item_id = MSIB.inventory_item_id --4018
            AND mtmt.organization_id = MSIB.organization_id --499
            AND mtmt.TRANSACTION_ACTION_ID NOT IN (24,30)
            AND to_date(mtmt.transaction_date) >= to_date(&Roll_back_date, 'DD-MON-RRRR')
        ) RB_TRANSACTIONS,
    mmt.annual_Consumption annual_Consumption,
    mmt.annual_Consumption / 4 months_Consumption,
    mmt.annual_Consumption / 12 monthly_Average,
    (
        SELECT MATERIAL_COST
        FROM CST_ITEM_COST_TYPE_V vw
        WHERE vw.organization_id = MSIB.organization_id
          AND vw.INVENTORY_ITEM_ID = MSIB.inventory_item_id
        ) Unit_Cost, --new
    sum(quan.t_quantity) - sum(r_quantity) finala
FROM mtl_system_items_b MSIB
LEFT  JOIN PO_HEADERS_ALL ph ON msib.inventory_item_id = pl.item_id
INNER JOIN Po_Lines_All pl ON ph.po_header_id = pl.po_header_id
INNER JOIN PO_LINE_LOCATIONS_ALL PLLA ON plla.po_header_id = ph.po_header_id AND pl.po_line_id = plla.po_line_id AND plla.org_id = pl.org_id
LEFT  JOIN 
    -------------------SUBQUERY---------------------------------------
    (
        SELECT nvl(sum(subplla.quantity), 0) t_quantity,
            nvl(sum(subplla.quantity_received), 0) r_quantity,
            subpl.item_id
        FROM po_headers_all subph 
        INNER JOIN po_lines_all subpl ON subph.po_header_id = subpl.po_header_id
        INNER JOIN po_line_locations_all subplla ON subplla.po_header_id = subph.po_header_id
                                                AND subpl.po_line_id = subplla.po_line_id
                                                AND subplla.org_id = subpl.org_id
        WHERE to_date(subplla.creation_date) >= to_date(&Roll_back_date, 'DD-MON-RRRR')
        GROUP BY subph.attribute1,
            subph.currency_code,
            subpl.item_id
        ) quan ON pl.item_id = quan.item_id
-------------------SUBQUERY---------------------------------------
LEFT JOIN   (
        SELECT mmt.ORGANIZATION_ID,
               mmt.INVENTORY_ITEM_ID,
               ABS(SUM(mmt.transaction_quantity)) AS annual_Consumption
        FROM MTL_MATERIAL_TRANSACTIONS mmt
        WHERE mmt.TRANSACTION_ACTION_ID NOT IN (24,30)
            AND mmt.TRANSACTION_TYPE_ID IN (33,52)
            AND mmt.transaction_date BETWEEN to_date(add_months(&CONS_f_DATE, - 12), 'DD-MON-YYYY')
                                         AND to_date(&CONS_f_DATE, 'DD-MON-YYYY')
            AND mmt.parent_transaction_id IS NULL
        ) mmt ON mmt.ORGANIZATION_ID = MSIB.organization_id --499--579
             AND mmt.INVENTORY_ITEM_ID = MSIB.inventory_item_id --4128 --4165 
WHERE msib.organization_id IN (
        SELECT haou.organization_id
        FROM hr_organization_information hoi
        JOIN hr_all_organization_units haou ON haou.organization_id = hoi.organization_id
        WHERE hoi.org_information1 = 'INV'
            AND hoi.org_information2 = 'Y'
            AND haou.name LIKE '%HEIS%'
        )
    AND MSIB.Inventory_Item_Id = NVL(&ITEM, MSIB.Inventory_Item_Id)
    AND MSIB.organization_id = nvl(&P_ORGI, MSIB.organization_id)
    AND to_date(plla.creation_date) BETWEEN to_date(add_months(&Roll_back_date, - 12), 'DD-MON-YYYY')
        AND to_date(&Roll_back_date, 'DD-MON-YYYY')
GROUP BY msib.segment1,
    MSIB.inventory_item_id,
    msib.description,
    MSIB.organization_id,
    msib.primary_unit_of_measure,
    ph.attribute1,
    ph.currency_code;

这是最简单的查询形式,主查询在没有子查询的情况下运行良好。但是,当子查询返回null(即日期范围内没有输出)时,整个查询将不返回任何内容。我希望它显示结果,而不考虑子查询结果。我猜JOIN有点问题——

       SELECT distinct msib.segment1          Item_Code,
               MSIB.inventory_item_id,
               MSIB.organization_id   ORG_ID,

               msib.description             Item_Description,
               msib.primary_unit_of_measure UOM,
               ph.attribute1                Item_Type,
               ph.currency_code currency,
               max(pl.unit_price) FOB_Value_in_FCY,
               max(ph.rate_date),
               max(ph.rate) forex_rate,

                    sum(quan.t_quantity) - sum(r_quantity) finala


FROM mtl_system_items_b    MSIB, 
               PO_HEADERS_ALL        ph,
               Po_Lines_All          pl,
               PO_LINE_LOCATIONS_ALL PLLA,

               ------------SUBQUERY-------------------------------
               (select nvl(sum(subplla.quantity),0) t_quantity, nvl(sum(subplla.quantity_received),0) r_quantity ,subpl.item_id

          from po_headers_all        subph,
               po_lines_all          subpl,
               po_line_locations_all subplla
         where subph.po_header_id = subpl.po_header_id
           and subplla.po_header_id = subph.po_header_id
           and subpl.po_line_id = subplla.po_line_id
           and subplla.org_id = subpl.org_id  
           and to_date(subplla.creation_date) >= to_date(&Roll_back_date,'DD-MON-RRRR')
         group by subph.attribute1, subph.currency_code, subpl.item_id       
         ) quan
          ------------SUBQUERY-------------------------------     
               WHERE 1=1
               and ph.po_header_id = pl.po_header_id
               and msib.inventory_item_id (+) = pl.item_id
               -----------------joining subquery-------------------
               and pl.item_id (+) = quan.item_id
               -----------------joining subquery-------------------
               and plla.po_header_id = ph.po_header_id
               and pl.po_line_id = plla.po_line_id
               and plla.org_id = pl.org_id

               and msib.organization_id in
                   (select haou.organization_id
                   from hr_organization_information hoi,
                   hr_all_organization_units   haou
                   where haou.organization_id = hoi.organization_id
                   and hoi.org_information1 = 'INV'
                   and hoi.org_information2 = 'Y'
                   and haou.name like '%HEIS%')

              and MSIB.Inventory_Item_Id=NVL(&ITEM,MSIB.Inventory_Item_Id)
              and MSIB.organization_id = nvl(&P_ORGI, MSIB.organization_id)
              AND to_date(plla.creation_date) BETWEEN
                  to_date(add_months(&Roll_back_date, -12),'DD-MON-YYYY') AND
                  to_date(&Roll_back_date,'DD-MON-YYYY')

GROUP BY  msib.segment1,
                  MSIB.inventory_item_id,
                  msib.description,
                  MSIB.organization_id,
                  msib.primary_unit_of_measure,
                  ph.attribute1, 
                  ph.currency_code               

为什么不使用SQL-92标准引入的联接?那是27年前的事了。这是很多SQL。考虑将代码简化为最短的代码段,这会导致问题的发生。SQL关键字应该全部是上或下,这样写是一个坏习惯。这里的查询是最简单的形式,主要查询在没有子查询的情况下工作得很好。但是,当子查询返回null(即日期范围内没有输出)时,整个查询将不返回任何内容。我希望它显示结果,而不考虑子查询结果。我猜Join有问题,我也尝试过新的Join,但仍然没有任何用处子查询不会阻止查询的其余部分返回结果。那里可能还有别的事情发生。