select 
          'BY SUBINVENTORY' subinventory_type,
          mos.subinventory_code wrh,
          mos.padded_concatenated_segments item,
          mos.item_description description,
          mos.primary_uom_code unit,
          nvl(total_qoh, 0) inventory_qty,
          mtb.attribute11 project_code,
          mos.inventory_item_id
from      mtl_system_items_b mtb,
            mtl_onhand_sub_v mos
where    (:p_from_subinventory is null or mos.subinventory_code >= :p_from_subinventory)
and      (:p_to_subinventory is null or mos.subinventory_code <= :p_to_subinventory)
and      nvl(mos.total_qoh,0) <> 0
and      mtb.inventory_item_id = mos.inventory_item_id
and      mtb.organization_id = mos.organization_id
and      mtb.organization_id = :p_organization_id
and      mos.subinventory_code in (select subinv 
                                   from suga_subinv 
                                   where type = :p_org_type)
union
select 
           'BY TRANX' subinventory_type,
           mtt.subinventory_code wrh,
           max(mtb.segment1) item,
           max(mtb.description) description,
           max(mtb.primary_uom_code) unit,  
           sum(nvl(mtt.primary_quantity,0)) inventory_qty,
           max(mtb.attribute11) project_code,
           mtb.inventory_item_id
from       mtl_material_transactions mtt,                 
           mtl_system_items_b mtb 
where   mtt.inventory_item_id = mtb.inventory_item_id 
and     mtb.organization_id = :p_organization_id
and     mtb.organization_id = mtt.organization_id
and     trunc(mtt.transaction_date) > :p_as_of_date
and     (:p_from_subinventory is null or mtt.subinventory_code >= :p_from_subinventory)
and     (:p_to_subinventory is null or mtt.subinventory_code <= :p_to_subinventory)
and    not exists (select  1 
                     from  mtl_onhand_sub_v mov
                    where  mov.inventory_item_id = mtb.inventory_item_id
                      and  mov.subinventory_code = mtt.subinventory_code)
and    mtt.subinventory_code in (select subinv 
                                 from suga_subinv 
                                 where type = :p_org_type)
group by
              mtt.subinventory_code,
              mtb.inventory_item_id
having sum(nvl(mtt.primary_quantity,0)) <> 0  希望让执行速度快点