Click here to Skip to main content
15,903,175 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Dear,

I have table called Trn_Purchase
fields are
ItemCode
ItemDesc
PurchaseDates
Price


I want to get the last purchase price depend on date

table have e.g below data

ItemCode PurchaseDate	Price 
A	01-Mar-2010 4 
A	02-Mar-2011 6
B	01-Mar-2010 7
B	02-Mar-2011 2


then result should be
itemCode	PurchaseDate	Price
A	02-Mar-2011 6
B	02-Mar-2011 2


How to do this?

Thanks
Basit.

What I have tried:

I did below.

SQL
SELECT  p.*
FROM
 Trn_Purchase p,
 (SELECT * FROM Trn_Purchase) m
WHERE 
 p.Itemcode = m.Itemcode
 and p.[Purchase Date] = m.[Purchase Date]
Posted
Updated 26-Apr-16 7:21am
v2

1 solution

All you need to do is to group data by ItemCode to get MAX(PurchaseDate) then join Price:

SQL
SELECT a.ItemCode, a.PurchaseDate, a.Price
FROM Trn_Purchase AS a INNER JOIN (
    SELECT  b.ItemCode, MAX(b.PurchaseDate) AS PurchaseDate 
    FROM Trn_Purchase AS b
    GROUP BY b.ItemCode
) AS c ON a.ItemCode = c.ItemCode AND a.PurchaseDate = c.PurchaseDate


For further information, please see:
MS Access: Max Function[^]
 
Share this answer
 
v4
Comments
basitsar 26-Apr-16 14:09pm    
Thanks Alot, but showing "You tried to execute a query that does not include the specific expression 'Price' as part of an aggregate function"
Maciej Los 26-Apr-16 14:17pm    
Check updated answer.
basitsar 27-Apr-16 0:28am    
Got error asking to enter parameter c.PurchaseDate popup coming
Maciej Los 27-Apr-16 5:37am    
Try again with new query.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900