0% found this document useful (0 votes)
84 views4 pages

5

This SQL query joins several tables to summarize inventory costing information. It retrieves inventory item details, quantities, unit costs, total costs and accounting amounts for a given cost organization, book, period and item. The results are grouped and sorted by category, organization, subinventory, item, valuation structure and other attributes.

Uploaded by

khaled_ghrbia
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
84 views4 pages

5

This SQL query joins several tables to summarize inventory costing information. It retrieves inventory item details, quantities, unit costs, total costs and accounting amounts for a given cost organization, book, period and item. The results are grouped and sorted by category, organization, subinventory, item, valuation structure and other attributes.

Uploaded by

khaled_ghrbia
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

WITH qty AS

(SELECT cost_org_id,
cost_book_id,
val_unit_id,
period_name,
inventory_org_id,
subinventory_code,
inventory_item_id,
MAX(uom_code) uom_code,
SUM(quantity_onhand) quantity_onhand,
SUM(starting_quantity) starting_quantity,
SUM(new_receipt_quantity) new_receipt_quantity,
SUM(new_issue_quantity) new_issue_quantity
FROM cst_attr_onhand_valuations
GROUP BY cost_org_id,
cost_book_id,
val_unit_id,
period_name,
inventory_org_id,
subinventory_code,
inventory_item_id

),
amt AS
(SELECT caod.cost_org_id,
caod.cost_book_id,
caod.val_unit_id,
caod.period_name,
caod.inventory_org_id,
caod.subinventory_code,
caod.inventory_item_id,
ce.cost_element_code cost_element_code,
MAX(caod.currency_code) currency_code,
MAX(caod.cost_method_code) cost_method_code,
SUM(caod.amount + caod.writeoff_amount) curr_total_costed_amount,
SUM(NVL(caod1.amount + caod1.writeoff_amount, 0))
prior_total_costed_amount,
SUM((caod.amount + caod.writeoff_amount) -
(NVL(caod1.amount + caod1.writeoff_amount, 0))) costed_amount,
SUM((caod.acctd_amount + caod.acctd_writeoff_amount) -
NVL((caod1.acctd_amount + caod1.acctd_writeoff_amount), 0))
acctd_amount,
SUM((((caod.amount + caod.writeoff_amount) -
NVL((caod1.amount + caod1.writeoff_amount), 0)) -
((caod.acctd_amount + caod.acctd_writeoff_amount +
caod.excl_from_acctg_amount + caod.excl_from_acctg_wo_amount) -
NVL((caod1.acctd_amount + caod1.acctd_writeoff_amount +
caod1.excl_from_acctg_amount +
caod1.excl_from_acctg_wo_amount),
0)))) AS unacctd_amount
FROM cst_attr_onhand_details caod,
cst_attr_onhand_details caod1,
cst_cost_elements_b ce
WHERE caod.cost_element_id = ce.cost_element_id
AND caod.prior_val_detail_id = caod1.val_detail_id(+)
AND caod.cost_org_id = caod1.cost_org_id(+)
AND caod.cost_book_id = caod1.cost_book_id(+)
GROUP BY caod.cost_org_id,
caod.cost_book_id,
caod.val_unit_id,
caod.period_name,
caod.inventory_org_id,
caod.subinventory_code,
caod.inventory_item_id,
ce.cost_element_code

),
org AS
(SELECT /*+ materialize */
hou.organization_id, hou.name organization_name
FROM hr_organization_units_f_tl hou
WHERE trunc(sysdate) BETWEEN hou.effective_start_date AND
hou.effective_end_date
AND language = userenv('LANG')),
ec AS
(SELECT eic.inventory_item_id,
eic.category_id,
eic.organization_id,
ecv.category_name,
ecv.category_code
FROM egp_item_categories eic,
egp_default_category_sets edc,
egp_categories_vl ecv
WHERE eic.category_set_id = edc.category_set_id
AND edc.functional_area_id = 5
AND eic.category_id = ecv.category_id)
SELECT /*+ leading(qty) USE_HASH(amt) */
gl.name ledger_name,
co.organization_name cost_org_name,
cb.cost_book_code,
vs.val_structure_code,
vu.val_unit_code,
qty.period_name,
io.organization_name inv_org_name,
qty.subinventory_code,
esi.item_number,
esi.description, /*Added this as part of Bug# 35381965*/
qty.inventory_item_id, /*Added this as part of Bug# 35381965*/
CASE
WHEN esi.item_type IS NOT NULL THEN
(SELECT meaning
from fnd_lookups
where lookup_type = 'EGP_ITEM_TYPE'
and lookup_code = esi.item_type)
ELSE
NULL
END AS item_type,
qty.uom_code,
amt.currency_code,
cm.meaning cost_method,
ec.category_name,
qty.quantity_onhand,
qty.starting_quantity,
qty.new_receipt_quantity,
qty.new_issue_quantity,
amt.cost_element_code cost_element_code,
-- DECODE(NVL(:p_cost_summarization, 'Y'), 'Y', qty.total_unit_cost, 'N',
amt.element_unit_cost) unit_cost,
-- qty.starting_quantity * amt.element_unit_cost starting_value,
-- qty.quantity_onhand * DECODE(NVL(:p_cost_summarization, 'Y'), 'Y',
qty.total_unit_cost, 'N', amt.element_unit_cost) onhand_value,
-- SUM(amt.curr_total_costed_amount)/nvl(qty.quantity_onhand,1) as
derived_unit_cost,
SUM(amt.curr_total_costed_amount) curr_total_costed_amount,
SUM(amt.prior_total_costed_amount) prior_total_costed_amount,
SUM(amt.costed_amount) costed_amount,
SUM(amt.acctd_amount) acctd_amount,
SUM(amt.unacctd_amount) unacctd_amount
FROM qty,
amt,
cst_cost_org_parameters ccop,
org co,
cst_cost_org_books cob,
cst_cost_books_b cb,
egp_system_items_vl esi, /*Replaced egp_system_items_b_v with
egp_system_items_vl as part of Bug# 35381965*/
org io,
cst_val_structures_b vs,
cst_val_units_b vu,
fnd_lookups cm,
ec,
gl_ledgers gl
WHERE qty.cost_org_id = amt.cost_org_id
AND qty.cost_org_id = ccop.cost_org_id
AND qty.cost_book_id = amt.cost_book_id
AND qty.period_name = amt.period_name
AND qty.inventory_item_id = amt.inventory_item_id
AND qty.inventory_org_id = amt.inventory_org_id
AND qty.subinventory_code = amt.subinventory_code
AND qty.val_unit_id = amt.val_unit_id
AND co.organization_id = qty.cost_org_id
AND co.organization_id = cob.cost_org_id
AND cb.cost_book_id = qty.cost_book_id
AND cob.cost_book_id = cb.cost_book_id
AND cob.ledger_id = gl.ledger_id(+)
-- AND qty.inventory_org_id = esi.organization_id
AND ccop.validation_organization_id = esi.organization_id(+) /*Replaced inner
join with outer join as part of Bug# 35381965*/
AND qty.inventory_item_id = esi.inventory_item_id(+) /*Replaced inner join with
outer join as part of Bug# 35381965*/
AND qty.inventory_org_id = io.organization_id
AND cm.lookup_type = 'CST_COST_METHODS'
AND cm.lookup_code = amt.cost_method_code

AND vs.val_structure_id = vu.val_structure_id

AND qty.val_unit_id = vu.val_unit_id


AND qty.inventory_item_id = ec.inventory_item_id(+)
-- AND qty.inventory_org_id = ec.organization_id (+)
AND ccop.validation_organization_id = ec.organization_id(+)
AND qty.inventory_item_id = 100000004062980
GROUP BY gl.name,
co.organization_name,
cb.cost_book_code,
vs.val_structure_code,
vu.val_unit_code,
qty.period_name,
io.organization_name,
qty.subinventory_code,
esi.item_number,
esi.description, /*Added this as part of Bug# 35381965*/
qty.inventory_item_id, /*Added this as part of Bug# 35381965*/
esi.item_type,
qty.uom_code,
amt.currency_code,
cm.meaning,
ec.category_name,
qty.quantity_onhand,
qty.starting_quantity,
qty.new_receipt_quantity,
qty.new_issue_quantity,
amt.cost_element_code
-- ,amt.element_unit_cost
ORDER BY ec.category_name,
io.organization_name,
qty.subinventory_code,
esi.item_number,
vs.val_structure_code,
vu.val_unit_code,
cm.meaning

You might also like