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_ID | ORG_ID | SEGMENT1 | DESCRIPTION | CURRENCY_CODE | CREATION_DATE | UOM | LAST_PURCHASE_PRICE |
| 8,100 | 82 | EXAL012 | AL. ROUND TUBE (ID: 11.4MM, OD: 18.3MM) | INR | 09/05/2011 17:52:04 | Kilogram | 182 |
| 8,100 | 82 | EXAL012 | AL. ROUND TUBE (ID: 11.4MM, OD: 18.3MM) | USD | 05/15/2012 16:02:36 | Kilogram | 8 |