I have a query which I want to list all latest transaction date for the purchase price for each stock item.
I used aggregate MAX() to list all latest transaction for each item without including the price and it works perfect. But when I included the price, the results was having multiple dates with different prices for each item.
So I try to use sub query but it resulted in error.
Below is my query using a sample item as a test
select
a.a7itno as "Item No", a.a7appr as "Unit Price",
b.maxtrdate as "Trans Date"
from m3edbprod.fcaavp a
Left Join
( select a7itno,max(a7trdt)as "maxtrdate"
from m3edbprod.fcaavp
group by a7itno) b
on a.a7itno=b.a7itno and a.a7trdt=b.maxtrdate
where a.a7itno='110ABC452'
The error appears when run:
Error: SQL0205 - Column MAXTRDATE not in table B in *N. (State:S0022, Native Code: FFFFFF33)
The Expected results should output each item with a single price and latest transdate such as tabulated below :
Item No Unit Price Trans Date
-----------------------------
110ABC452 100.00 20210920
Note: the date is in YYYYMMDD, if I can set it to date format like 20/09/2021 or 20-09-2021 will also be good.
Will appreciate if I could get some advise from here.