Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Last Purchase price based on Max Creation date

Aviral_14Dec 25 2019 — edited Dec 27 2019

I need only the line with the max creation date .

Now if i am adding the attribute currency code it is giving two line because the purchases are made in both the Cureencies.

Instead the result should be based only on the latest purchased date irrespective of the currency code.

Query  - -

select DISTINCT msi.inventory_item_id,pla.org_id,msi.segment1,MSI.DESCRIPTION,PHA.CURRENCY_CODE,

max(pha.creation_date) as creation_date,

Msi.Primary_Unit_Of_Measure UOM,min(pla.unit_price)

keep (dense_rank last order by pha.creation_date) as last_purchase_price

from  po_lines_all pla

join po_headers_all   pha   on pla.po_header_id = pha.po_header_id AND PLA.ORG_ID = PHA.ORG_ID

join ap_suppliers     ass   on ass.vendor_id    = pha.vendor_id

join mtl_system_items msi   on pla.item_id      = msi.inventory_item_id

where  pha.creation_date >= to_date('01-APR-2010', 'dd-MON-yyyy')

and  pla.org_id = 82

AND MSI.ORGANIZATION_ID = 86

AND MSI.segment1 = 'EXAL012'

group  by msi.inventory_item_id, msi.segment1,Msi.Primary_Unit_Of_Measure,

MSI.DESCRIPTION,pla.org_id,PHA.CURRENCY_CODE

-- order by 1, 2   -- if needed ;

Output --

        

INVENTORY_ITEM_IDORG_IDSEGMENT1DESCRIPTIONCURRENCY_CODECREATION_DATEUOMLAST_PURCHASE_PRICE
8,10082EXAL012AL. ROUND TUBE (ID: 11.4MM, OD: 18.3MM)INR09/05/2011 17:52:04Kilogram182
8,10082EXAL012AL. ROUND TUBE (ID: 11.4MM, OD: 18.3MM)USD05/15/2012 16:02:36Kilogram8
Comments
Post Details
Added on Dec 25 2019
3 comments
1,394 views