oracle产销存的写法
with TEMP as
(select sum(MMT.TRANSACTION_QUANTITY) QTY_QC, MMT.INVENTORY_ITEM_ID --,CAH.Legal_Entity_Id,CAH.COST_GROUP_ID,CAH.COST_TYPE_ID
from MTL_MATERIAL_TRANSACTIONS MMT, CST_AE_HEADERS CAH --,CST_AE_LINES CAL
where MMT.TRANSACTION_QUANTITY <> 0
AND MMT.TRANSACTION_TYPE_ID NOT IN (2, 52, 10008)
AND MMT.INVENTORY_ITEM_ID = nvl(:P_ITEM_ID, mmt.inventory_item_id) --100528
AND CAH.ACCOUNTING_DATE = MMT.TRANSACTION_DATE
AND CAH.accounting_event_id = MMT.TRANSACTION_ID
AND CAH.ACCOUNTING_DATE > to_date('2013-04-27', 'YYYY-MM-DD') --oracle开账日期
AND CAH.ACCOUNTING_DATE < to_date(:P_START_DATE, 'YYYY-MM-DD')
AND CAH.ACCT_EVENT_SOURCE_TABLE = 'MMT'
AND CAH.COST_GROUP_ID = :P_COST_GROUP
AND CAH.LEGAL_ENTITY_ID = :P_LEGAL_ENTITY
GROUP BY MMT.INVENTORY_ITEM_ID)
select CCG.COST_GROUP COST_GROUP,
CCG.DESCRIPTION COST_DESCRIPTION,
:P_START_DATE START_DATE, ------起始日期 ///////////时间参数
:P_END_DATE END_DATE, ------截止日期 ////////////时间参数
GCC.SEGMENT3,
GL_FLEXFIELDS_PKG.GET_DESCRIPTION_SQL(GCC.CHART_OF_ACCOUNTS_ID,
3,
GCC.SEGMENT3) SEGMENT3_DESC,
MSI.SEGMENT1 INVENTORY_ITEM_NO, -----物料编码
MSI.DESCRIPTION INVENTORY_ITEM_DESCRIPTION, -----物料描述
MSI.ATTRIBUTE12 PRIDUCTLINE, --产品线
NULL ACC_DESCRIPTION, -----帐户类别
A.QTY_QC, --期初数量
DECODE(A.QTY_QC, 0, 0, ROUND(B.ACCOUNTED_QC / A.QTY_QC, 6)) UP_QC, -----期初单位成本
B.ACCOUNTED_QC, --期初金额
C.QTY_ENTER,
DECODE(C.QTY_ENTER, 0, 0, ROUND(D.ACCOUNTED_DR / C.QTY_ENTER, 6)) UP_DR, -----本期入库单位成本
D.ACCOUNTED_DR,
C.QTY_OUT,
DECODE(C.QTY_OUT, 0, 0, ROUND(D.ACCOUNTED_CR / C.QTY_OUT, 6)) UP_CR, -----本期出库单位成本
D.ACCOUNTED_CR,
A.QTY_QC - C.QTY_ENTER - C.QTY_OUT QTY_QM, -----期末结存数量
DECODE((A.QTY_QC + C.QTY_ENTER - C.QTY_OUT),
0,
0,
ROUND((B.ACCOUNTED_QC + D.ACCOUNTED_DR - D.ACCOUNTED_CR) /
(A.QTY_QC + C.QTY_ENTER - C.QTY_OUT),
6)) UP_QM, -----期末结存单位成本
B.ACCOUNTED_QC + D.ACCOUNTED_DR - D.ACCOUNTED_CR ACCOUNTED_QM, -----期末结存成本金额
GL_FLEXFIELDS_PKG.GET_CONCAT_DESCRIPTION(GCC.CHART_OF_ACCOUNTS_ID,
GCC.CODE_COMBINATION_ID) ACCOUNT_DESC -----会计科目组合描述
from TEMP A,
--期初数量
--期初金额
(SELECT sum(nvl(cal.accounted_dr, 0) - nvl(cal.accounted_cr, 0)) ACCOUNTED_QC,
mmt.inventory_item_id,
CAL.CODE_COMBINATION_ID
FROM CST_AE_HEADERS CAH,
MTL_MATERIAL_TRANSACTIONS MMT,
CST_AE_LINES CAL,
GL_CODE_COMBINATIONS GCC,
FND_FLEX_VALUES FFVS,
CST_COST_GROUPS CCG,
TEMP T
where cah.ae_header_id = cal.ae_header_id
and CAL.DESCRIPTION IN ('库存估价', 'Inv valuation') -----------
AND CAL.SOURCE_TABLE = 'MMT'
and MMT.TRANSACTION_TYPE_ID NOT IN (2, 52, 10008)
AND CAH.ACCOUNTING_DATE = MMT.TRANSACTION_DATE
AND CAH.accounting_event_id = MMT.TRANSACTION_ID
and CAH.ACCOUNTING_DATE > to_date('2013-04-27', 'YYYY-MM-DD') --oracle开账日期
AND CAH.ACCOUNTING_DATE < to_date(:P_START_DATE, 'YYYY-MM-DD')
AND CAH.ACCT_EVENT_SOURCE_TABLE = 'MMT'
AND CAH.COST_GROUP_ID = :P_COST_GROUP
AND CAH.LEGAL_ENTITY_ID = :P_LEGAL_ENTITY
AND FFVS.ATTRIBUTE2 = '存货核算'
AND FFVS.VALUE_CATEGORY = 'GL_MEG_ACCOUNT'
AND GCC.SEGMENT3 = FFVS.FLEX_VALUE
AND GCC.CODE_COMBINATION_ID = CAL.CODE_COMBINATION_ID
and CCG.COST_GROUP_ID = CAH.COST_GROUP_ID
AND T.INVENTORY_ITEM_ID = MMT.INVENTORY_ITEM_ID
group by mmt.inventory_item_id, CAL.CODE_COMBINATION_ID) B,
--本期入库数量、本期出库数量
(select sum(case
when MMT.TRANSACTION_QUANTITY > 0 then
MMT.TRANSACTION_QUANTITY
else
0
end) QTY_ENTER,
sum(case
when MMT.TRANSACTION_QUANTITY < 0 then
-MMT.TRANSACTION_QUANTITY
else
0
end) QTY_OUT,
MMT.INVENTORY_ITEM_ID --,CAH.Legal_Entity_Id,CAH.COST_GROUP_ID,CAH.COST_TYPE_ID
from MTL_MATERIAL_TRANSACTIONS MMT, CST_AE_HEADERS CAH, TEMP T --,
-- CST_AE_LINES CAL
where MMT.TRANSACTION_QUANTITY <> 0
AND MMT.TRANSACTION_TYPE_ID NOT IN (2, 52, 10008)
--AND MMT.INVENTORY_ITEM_ID=100528
AND CAH.ACCOUNTING_DATE = MMT.TRANSACTION_DATE
AND CAH.accounting_event_id = MMT.TRANSACTION_ID
AND CAH.ACCOUNTING_DATE >= to_date(:P_START_DATE, 'YYYY-MM-DD') --oracle开账日期
AND CAH.ACCOUNTING_DATE < to_date(:P_END_DATE, 'YYYY-MM-DD') + 1
AND CAH.ACCT_EVENT_SOURCE_TABLE = 'MMT'
AND CAH.COST_GROUP_ID = :P_COST_GROUP
AND CAH.LEGAL_ENTITY_ID = :P_LEGAL_ENTITY
AND T.INVENTORY_ITEM_ID = MMT.INVENTORY_ITEM_ID
GROUP BY MMT.INVENTORY_ITEM_ID) C,
--本期入库金额、本期入库金额
(SELECT sum(nvl(cal.accounted_dr, 0)) ACCOUNTED_DR,
SUM(nvl(cal.accounted_cr, 0)) ACCOUNTED_CR,
mmt.inventory_item_id
FROM CST_AE_HEADERS CAH,
MTL_MATERIAL_TRANSACTIONS MMT,
CST_AE_LINES CAL,
TEMP T
where CAL.DESCRIPTION IN ('库存估价', 'Inv valuation') -----------
AND CAL.SOURCE_TABLE = 'MMT'
AND cah.ae_header_id = cal.ae_header_id
and MMT.TRANSACTION_TYPE_ID NOT IN (2, 52, 10008)
--AND MMT.INVENTORY_ITEM_ID=100528
AND CAH.ACCOUNTING_DATE = MMT.TRANSACTION_DATE
AND CAH.accounting_event_id = MMT.TRANSACTION_ID
and CAH.ACCOUNTING_DATE >= to_date(:P_START_DATE, 'YYYY-MM-DD') --oracle开账日期
AND CAH.ACCOUNTING_DATE < to_date(:P_END_DATE, 'YYYY-MM-DD') + 1
AND CAH.ACCT_EVENT_SOURCE_TABLE = 'MMT'
AND CAH.COST_GROUP_ID = :P_COST_GROUP
AND CAH.LEGAL_ENTITY_ID = :P_LEGAL_ENTITY
AND T.INVENTORY_ITEM_ID = MMT.INVENTORY_ITEM_ID
group by mmt.inventory_item_id) D,
mtl_system_items_b MSI,
CST_COST_GROUPS CCG,
GL_CODE_COMBINATIONS GCC
WHERE CCG.COST_GROUP_ID = :P_COST_GROUP
and CCG.LEGAL_ENTITY = :P_LEGAL_ENTITY
AND A.inventory_item_id = B.inventory_item_id(+)
AND A.inventory_item_id = C.inventory_item_id(+)
AND A.inventory_item_id = D.inventory_item_id(+)
AND A.inventory_item_id = MSI.inventory_item_id
AND MSI.ORGANIZATION_ID = 82
and B.CODE_COMBINATION_ID = GCC.CODE_COMBINATION_ID
AND (A.QTY_QC <> 0 OR B.ACCOUNTED_QC <> 0)