SELECT company.flex_value,
company.description,
inv.item_code,
--inv.lot_number,--批次
inv.quantity
FROM (SELECT ffvt.description, ffv.flex_value, ffv.attribute1
FROM fnd_flex_value_sets ffvs,
fnd_flex_values ffv,
fnd_flex_values_tl ffvt
WHERE ffvs.flex_value_set_id = ffv.flex_value_set_id
AND ffv.flex_value_id = ffvt.flex_value_id
AND ffvt.language = 'ZHS'
AND ffv.summary_flag = 'N'
AND ffv.enabled_flag = 'Y'
AND ffvs.flex_value_set_name = 'BZ_COMPANY') company,
(SELECT v.flex_value company_code,
msib.segment1 item_code,
--mtln.lot_number,--批次
SUM(mtln.transaction_quantity) quantity
FROM mtl_material_transactions mmt,
mtl_transaction_lot_numbers mtln,
mtl_system_items_b msib,
fnd_flex_value_sets vs,
fnd_flex_values v
WHERE mmt.transaction_id = mtln.transaction_id
AND mmt.subinventory_code LIKE '%-INV'
AND mtln.lot_number <> 'EBS_20131231'
AND mmt.transaction_date < to_date('20140201', 'YYYYMMDD')
--and mmt.transaction_date >= to_date('2014-01-01','YYYY-MM-DD')
AND flex_value_set_name = 'BZ_COMPANY'
AND v.flex_value_set_id = vs.flex_value_set_id
AND v.attribute1 = mmt.organization_id
AND msib.inventory_item_id = mmt.inventory_item_id
AND msib.organization_id = 82
-- and msib.segment1 = 'TLJW1067'--物料
GROUP BY v.flex_value, msib.segment1 /*, mtln.lot_number*/ --批次
) inv
WHERE inv.company_code = company.flex_value