|
我現在已經優化成這樣了,可運行了4小時還沒出來
select a.item,
a.des,
a.loc,
a.cla,
a.sub,
sum(a.qty),
a.uom,
a.t_date2,
a.factory,
b.max_date
from (SELECT /*+rule */
MSI.SEGMENT1 ITEM,
MSI.DESCRIPTION DES,
C.SEGMENT1 LOC,
mc.segment1 cla,
mmt.subinventory_code sub,
mmt.transaction_quantity qty,
mmt.transaction_uom UOM,
to_char(MMT.TRANSACTION_DATE, 'yyyy-mm-dd') T_DATE2,
decode(substr(msi.unit_length, 1, 1),
1,
'一厂',
2,
'二厂',
3,
'三厂',
6,
'開發部',
8,
'外協',
'無') factory
from inv.MTL_MATERIAL_TRANSACTIONS MMT,
inv.MTL_SYSTEM_ITEMS_B MSI,
inv.mtl_item_categories mic,
INV.MTL_ITEM_LOCATIONS C,
inv.MTL_CATEGORIES_B MC
WHERE C.INVENTORY_LOCATION_ID(+) = MMT.LOCATOR_ID
and mmt.inventory_item_id = MSI.INVENTORY_ITEM_ID
and mic.inventory_item_id = msi.inventory_item_id
and mic.category_id = mc.category_id) a,
( ----倉庫進倉最後一筆
select item1, sub1, loc1, max_date
from (select row_number() over(partition by a.item1, a.sub1, a.loc1 order by a.max_date desc) no,
a.item1,
a.sub1,
a.loc1,
a.max_date
from (select t.transaction_date max_date,
msb.segment1 item1,
t.subinventory_code sub1,
mil1.segment1 loc1
from inv.mtl_material_transactions t,
inv.mtl_system_items_b msb,
inv.mtl_item_locations mil1
where t.inventory_item_id = msb.inventory_item_id
and t.locator_id = mil1.inventory_location_id
and msb.inventory_item_status_code = 'Active'
and t.transaction_quantity > 0) a)
where no = 1) b
where a.item = b.item1
and a.sub = b.sub1
and a.loc = b.loc1
group by a.item,
a.des,
a.loc,
a.cla,
a.sub,
a.uom,
a.t_date2,
a.factory,
b.max_date
|
|