Я хочу добавить подзапрос с параметром даты, чтобы вернуть вычисленное значение, но мой запрос не работает - PullRequest
0 голосов
/ 26 апреля 2019

У меня есть запрос, который выбирает данные из инвентаря оракула и покупки.Теперь я хочу добавить подзапрос из po_line_locations_all с параметром даты, чтобы возвращать вычисленное значение, как если бы значение находилось в подзапросе, показывать, что оно отображает ноль, но в моем случае запрос ничего не показывает, если подзапрос возвращает ноль.

Я хочу показать результат основного запроса, даже если подзапрос возвращает ноль.

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

Ответы [ 2 ]

1 голос
/ 26 апреля 2019

Полагаю, ваша проблема заключается в том, что вы просто используете соединения старого синтаксиса вместо того, что было очень давно.

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;
0 голосов
/ 27 апреля 2019

Вот запрос в его простейшей форме. Основной запрос работает хорошо без подзапроса.Но когда подзапрос возвращает ноль (т. Е. Нет выходных данных в диапазоне дат), весь запрос ничего не возвращает.Я хочу, чтобы он отображал результаты независимо от результатов подзапроса.Я думаю, что-то не так с 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               
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...