I need to query a DB2 table to get the latest data stored in the table by each unique Sup_Num and CONTAINER_CODE combination. The problem I'm having is that I can't figure out how to get only the data with the most recent date (INSERTED_DT). I tried using the Max() function on the INSERTED_DT field, but it gave me the exact same result I get without using the Max() function. I didn't see anything like this when I searched for Max() function usage examples so I'm hoping someone on here could help.
The query I am using is this:
Select Distinct
SupLookup.SUPPLIER_NO concat SupLookup.SUPPLIER_LOCATION AS Sup_Num,
SupLookup.CONTAINER_CODE,
AllocationType.ALLOC_TYPE_DESC,
Allocation.ALLOC_QTY,
Allocation.SAFE_STOCK_QTY,
Allocation.ALLOC_QTY + Allocation.SAFE_STOCK_QTY AS CALC_TOT_ALLOC_REQ_QTY,
SupLookup.ALLOC_REQ_QTY,
SupLookup.TOT_ALLOC_REQ_QTY,
SupLookup.DISC_QTY,
SupLookup.FILL_PERCENT,
MAX(SupLookup.INSERTED_DT) as Insert_DT
From RCX.RXSAL1 Allocation
Inner Join rcx.RXALT1 AllocationType on Allocation.ALLOC_TYPE_ID = AllocationType.ALLOC_TYPE_ID
Left Join rcx.RXPIR1 SupLookup on Allocation.SUPPLIER_ID = SupLookup.SUPPLIER_ID And allocation.CONTAINER_TYPE_ID = SupLookup.CONTAINER_TYPE_ID
Where Allocation.PLANT_ID= '50000036'
Group by
SupLookup.SUPPLIER_NO concat SupLookup.SUPPLIER_LOCATION,
SupLookup.CONTAINER_CODE,
AllocationType.ALLOC_TYPE_DESC,
Allocation.ALLOC_QTY,
Allocation.SAFE_STOCK_QTY,
Allocation.ALLOC_QTY + Allocation.SAFE_STOCK_QTY,
SupLookup.ALLOC_REQ_QTY,
SupLookup.TOT_ALLOC_REQ_QTY,
SupLookup.DISC_QTY,
SupLookup.FILL_PERCENT
ORDER BY Sup_Num ASC
What I'm getting looks like this:
But what I want is for it to pick out just the most recent date for each combination of Sup_Num and Container_Code like this:
-EDIT-
The simple max/group by sub query here fails in this instance (server timeout). Someone suggested it at one point but has since removed the post.