網上的一篇文章:
Oracle Applications 提供了計算庫存的表 MTL_ONHAND_QUANTITIES_DETAIL,開發人員只要按要求對錶進行分組求和即可得到庫存的現有量,這個數量就是我們在系統中查詢的數量。但是如果用戶要開發人員提供上個月或上個季度的庫存就麻煩了。因爲表MTL_ONHAND_QUANTITIES_DETAIL不提供追溯庫存計算的方法。那怎麼解決這個問題?
我們先來看看錶MTL_ONHAND_QUANTITIES_DETAIL和 mtl_material_transactions 表的關係。mtl_material_transactions 是 Oracle Applications 庫存的基表,凡是和庫存有關的數據都必須存入這個表,如初期的庫存導入,採購的接受入庫。銷售,製造的完工入庫。子庫轉移等。mtl_material_transactions 的分組求和應該等於MTL_ONHAND_QUANTITIES_DETAIL表的分組求和,但表mtl_material_transactions
的記錄一般數據龐大,不會在這個表中求現有量。mtl_material_transactions表中每筆的數據異動都會影響到MTL_ONHAND_QUANTITIES_DETAIL 的最終結果。我們通過研究發現。mtl_material_transactions 每筆入庫的記錄都會相應的在MTL_ONHAND_QUANTITIES_DETAI 表中增加此記錄。在庫存未消失前我們可以通過 mtl_material_transactions表的TRANSACTION_ID字段和表MTL_ONHAND_QUANTITIES_DETAIL的字段CREATE_TRANSACTION_ID
建立一一對應關係。
現在的問題是我在系統裏通過MTL_ONHAND_QUANTITIES_DETAIL中的CREATE_TRANSACTION_ID字段 關聯mtl_material_transactions只能關聯到一小部分,有很多都是空的,mtl_material_transactions就沒有CREATE_TRANSACTION_ID 的記錄,求解
這個是按年份計算曆史庫存狀態的查詢,可以按自己要求改寫出你要的語句。
select organization_id,
to_char(lastdate, 'yyyy'),
count(*) cnt,
sum(amount) value
from (
select tran.organization_id,
tran.inventory_item_id,
cict.item_number,
cict.description,
cict.primary_uom_code,
round(item_cost, 2) item_cost,
onhand,
round(onhand * item_cost, 2) amount,
lastdate,
planning_make_buy_code
from (select sum(primary_transaction_quantity) onhand,
inventory_item_id,
organization_id
from mtl_onhand_quantities_detail
where organization_id = 1
group by inventory_item_id, organization_id) moq,
(select max(transaction_date) lastDate,
inventory_item_id,
organization_id
from inv.mtl_material_transactions
where transaction_type_id not in (10008, 2, 90, 24)
and organization_id = 1
group by inventory_item_id, organization_id) tran,
(select inventory_item_id,
item_number,
description,
primary_uom_code,
planning_make_buy_code,
max(item_cost) item_cost
from CST_ITEM_COST_TYPE_V
where (organization_id in (1, 2, 3)
and cost_type_id = 1 or organization_id =4
and cost_type_id = 2)
and item_cost > 0
group by inventory_item_id,
item_number,
description,
primary_uom_code,
planning_make_buy_code) cict
where tran.inventory_item_id = moq.inventory_item_id
and tran.organization_id = moq.organization_id
and cict.inventory_item_id(+) = moq.inventory_item_id
and lastdate < to_date('2011-01-01', 'yyyy-mm-dd')
order by lastdate, item_number
)
group by organization_id, to_char(lastdate, 'yyyy')
order by organization_id, to_char(lastdate, 'yyyy')
查歷史庫存不能使用MTL_ONHAND_QUANTITIES之類的表,只能做覈對工作,語句核心是transaction_type_id not in (10008, 2, 90, 24),要排除這幾種事務類型。留下的都是影響庫存數量的事務。
注意的是當歷史上事務處理出現異常時,最後計算出來的現有量會與MTL_ONHAND_QUANTITIES表中不一致,可以用當前現有量覈對不一致的物料。使用這個語句
select * from (select sum(primary_quantity) qty,
inventory_item_id,
organization_id
from inv.mtl_material_transactions
where transaction_type_id not in(10008,2,90,24)
and organization_id = 2
group by inventory_item_id, organization_id) tran,
(select organization_id,
inventory_item_id,
sum(on_hand) quantity
from MTL_ONHAND_TOTAL_V where organization_id = 2
group by organization_id, inventory_item_id) Mot
where tran.inventory_item_id=mot.inventory_item_id
and tran.organization_id=mot.organization_id
and tran.qty<>mot.quantity
。查詢可能會慢些,但是能接受。