QUOTE:
原帖由
镶黄旗 于 2008-7-16 18:17 发表

平均库存=当前库存-sum(此前31天每天的净发生额*(31 - 所在日) / 31天)
-------------------1.查某月平均库存----------------
select
mmt.organization_id 组织,
mmt.subinventory_code 子库,
mmt.inventory_item_id 物料,
--数量*(当月天数-当日)/当月天数
-sum(mmt.primary_quantity)*(to_char(last_day(to_date('2007-03-31','YYYY-MM-DD')),'dd')-to_char(mmt.transaction_date,'dd'))/to_char(last_day(to_date('2007-03-31','YYYY-MM-DD')),'dd') 数量
from mtl_material_transactions mmt
where 1=1
AND mmt.transaction_type_id NOT IN (80,24,25)
and mmt.organization_id=85
and mmt.subinventory_code='EPLP01'
--and mmt.inventory_item_id=2800
and mmt.transaction_date between to_date('2007-03-01 00:00:00', 'YYYY-MM-DD HH24:MI:SS')
and to_date('2007-03-31 23:59:59', 'YYYY-MM-DD HH24:MI:SS')
group by
mmt.organization_id ,
mmt.subinventory_code ,
mmt.inventory_item_id,
to_char(mmt.transaction_date,'dd')
union
-----------------------------------------------------
------------2.取某个物料当前库存数量
SELECT
moq.ORGANIZATION_ID 组织,
moq.SUBINVENTORY_CODE 子库,
moq.INVENTORY_ITEM_ID 物料,
transaction_quantity 数量--这里的当前库存量
FROM mtl_onhand_quantities moq
WHERE moq.organization_id = 85
AND moq.subinventory_code = 'EPLP01'
-- AND moq.inventory_item_id = 2800