SELECT msi.segment1 itemcode,msi.description fg_descr, msi.primary_unit_of_measure fg_uom,
ROUND(ABS((SELECT SUM (mmt1.transaction_quantity)
FROM mtl_material_transactions mmt1
WHERE trunc(mmt1.transaction_date) = :from_start_date
AND mmt1.transaction_type_id = mtt.transaction_type_id
AND mmt1.inventory_item_id = msi.inventory_item_id
AND mmt1.organization_id = msi.organization_id)),3) prod_from_date,
ROUND(ABS(SUM (mmt.transaction_quantity)),3) prod_date_range,
ROUND((SELECT ABS(SUM (mmt1.transaction_quantity))
FROM mtl_material_transactions mmt1
WHERE 1=1
AND mmt1.transaction_type_id = mtt.transaction_type_id
AND mmt1.inventory_item_id = msi.inventory_item_id
AND mmt1.organization_id = msi.organization_id
AND mmt1.transaction_date BETWEEN
(SELECT start_date
FROM cm_cldr_dtl
WHERE calendar_code =
(SELECT calendar_code
FROM cm_cldr_dtl
WHERE :from_start_date BETWEEN start_date AND end_date
AND calendar_code = 'XX_CST_CL')
AND period_code = 1)
AND (SELECT end_date
FROM cm_cldr_dtl
WHERE calendar_code =
(SELECT calendar_code
FROM cm_cldr_dtl
WHERE :from_start_date BETWEEN start_date AND end_date
AND calendar_code = 'XX_CST_CL')
AND period_code = 12)),3) prod_year
FROM mtl_material_transactions mmt,
mtl_transaction_types mtt,
mtl_system_items_b msi,
gme_batch_header gbh
WHERE trunc(mmt.transaction_date) BETWEEN :from_start_date AND :to_start_date
AND msi.organization_id = :p_org_id
AND mmt.transaction_type_id = mtt.transaction_type_id
AND mtt.transaction_type_name = 'WIP Issue'
AND msi.inventory_item_id = NVL(:p_item_id,msi.inventory_item_id)
AND mmt.inventory_item_id = msi.inventory_item_id
AND mmt.organization_id = msi.organization_id
AND mmt.transaction_source_id = gbh.batch_id
AND msi.item_type = 'FG'
AND EXISTS
(SELECT 1
FROM gme_material_details gmd,gme_batch_header gbh
WHERE 1=1
AND gmd.inventory_item_id = msi.inventory_item_id
AND gmd.batch_id = gbh.batch_id
AND gmd.organization_id = msi.organization_id
AND line_type = -1
AND msi.inventory_item_id in
(SELECT gmd1.inventory_item_id
FROM gme_material_details gmd1,gme_batch_header gbh1
WHERE gbh1.batch_no = NVL(:P_BATCH_NO,gbh1.batch_no)
AND gmd1.batch_id = gbh1.batch_id
AND gmd1.line_type in (1,2)))
GROUP BY msi.segment1,msi.description, msi.primary_unit_of_measure,mtt.transaction_type_id,msi.inventory_item_id,msi.organization_id
ORDER BY msi.segment1
ROUND(ABS((SELECT SUM (mmt1.transaction_quantity)
FROM mtl_material_transactions mmt1
WHERE trunc(mmt1.transaction_date) = :from_start_date
AND mmt1.transaction_type_id = mtt.transaction_type_id
AND mmt1.inventory_item_id = msi.inventory_item_id
AND mmt1.organization_id = msi.organization_id)),3) prod_from_date,
ROUND(ABS(SUM (mmt.transaction_quantity)),3) prod_date_range,
ROUND((SELECT ABS(SUM (mmt1.transaction_quantity))
FROM mtl_material_transactions mmt1
WHERE 1=1
AND mmt1.transaction_type_id = mtt.transaction_type_id
AND mmt1.inventory_item_id = msi.inventory_item_id
AND mmt1.organization_id = msi.organization_id
AND mmt1.transaction_date BETWEEN
(SELECT start_date
FROM cm_cldr_dtl
WHERE calendar_code =
(SELECT calendar_code
FROM cm_cldr_dtl
WHERE :from_start_date BETWEEN start_date AND end_date
AND calendar_code = 'XX_CST_CL')
AND period_code = 1)
AND (SELECT end_date
FROM cm_cldr_dtl
WHERE calendar_code =
(SELECT calendar_code
FROM cm_cldr_dtl
WHERE :from_start_date BETWEEN start_date AND end_date
AND calendar_code = 'XX_CST_CL')
AND period_code = 12)),3) prod_year
FROM mtl_material_transactions mmt,
mtl_transaction_types mtt,
mtl_system_items_b msi,
gme_batch_header gbh
WHERE trunc(mmt.transaction_date) BETWEEN :from_start_date AND :to_start_date
AND msi.organization_id = :p_org_id
AND mmt.transaction_type_id = mtt.transaction_type_id
AND mtt.transaction_type_name = 'WIP Issue'
AND msi.inventory_item_id = NVL(:p_item_id,msi.inventory_item_id)
AND mmt.inventory_item_id = msi.inventory_item_id
AND mmt.organization_id = msi.organization_id
AND mmt.transaction_source_id = gbh.batch_id
AND msi.item_type = 'FG'
AND EXISTS
(SELECT 1
FROM gme_material_details gmd,gme_batch_header gbh
WHERE 1=1
AND gmd.inventory_item_id = msi.inventory_item_id
AND gmd.batch_id = gbh.batch_id
AND gmd.organization_id = msi.organization_id
AND line_type = -1
AND msi.inventory_item_id in
(SELECT gmd1.inventory_item_id
FROM gme_material_details gmd1,gme_batch_header gbh1
WHERE gbh1.batch_no = NVL(:P_BATCH_NO,gbh1.batch_no)
AND gmd1.batch_id = gbh1.batch_id
AND gmd1.line_type in (1,2)))
GROUP BY msi.segment1,msi.description, msi.primary_unit_of_measure,mtt.transaction_type_id,msi.inventory_item_id,msi.organization_id
ORDER BY msi.segment1