Solved latest price (1 Viewer)

FahadTiger

Member
Local time
Today, 21:52
Joined
Jun 20, 2021
Messages
115
Hello My Expert
I have tblPurchase with PurchaseID AS primary key and purchaseDate
and tblPurchaseDetail with PDID AS primary key and PurchaseID as number and ItemID AS number
and purchasePrice as currency...what I want the latest purchasePrice for each Item according to latest purchaseDate
any advise ...thank you
 

June7

AWF VIP
Local time
Today, 10:52
Joined
Mar 9, 2014
Messages
5,473
Possibly with a correlated subquery using TOP N. Review http://allenbrowne.com/subquery-01.html#TopN
But I think your requirement may be more complicated.

Want to provide db for analysis?

I advise not to use exact same field name in multiple tables. Perhaps PurchaseID_FK in tblPurchaseDetail.

Try this.

Build and save a query that joins tables, name it Purchases_Details:

SELECT tblPurchaseDetail.PDID, tblPurchaseDetail.PurchaseID, tblPurchaseDetail.ItemID, tblPurchaseDetail.purchasePrice, tblPurchase.PurchaseDate
FROM tblPurchase INNER JOIN tblPurchaseDetail ON tblPurchase.PurchaseID = tblPurchaseDetail.PurchaseID;

Use that query in another:

SELECT Purchases_Details.*
FROM Purchases_Details WHERE PDID IN (SELECT TOP 1 PDID FROM Purchases_Details AS Dupe WHERE Dupe.ItemID=Purchases_Details.ItemID
ORDER BY Dupe.PurchaseDate DESC, Dupe.PDID DESC);
 
Last edited:

XPS35

Active member
Local time
Today, 20:52
Joined
Jul 19, 2022
Messages
159
And what if the item has not been purchased before?
 
Last edited:

mike60smart

Registered User.
Local time
Today, 19:52
Joined
Aug 6, 2017
Messages
1,906
Hello My Expert
I have tblPurchase with PurchaseID AS primary key and purchaseDate
and tblPurchaseDetail with PDID AS primary key and PurchaseID as number and ItemID AS number
and purchasePrice as currency...what I want the latest purchasePrice for each Item according to latest purchaseDate
any advise ...thank you
Can you upload a zipped copy of your database?
 

FahadTiger

Member
Local time
Today, 21:52
Joined
Jun 20, 2021
Messages
115
Possibly with a correlated subquery using TOP N. Review http://allenbrowne.com/subquery-01.html#TopN
But I think your requirement may be more complicated.

Want to provide db for analysis?

I advise not to use exact same field name in multiple tables. Perhaps PurchaseID_FK in tblPurchaseDetail.

Try this.

Build and save a query that joins tables, name it Purchases_Details:

SELECT tblPurchaseDetail.PDID, tblPurchaseDetail.PurchaseID, tblPurchaseDetail.ItemID, tblPurchaseDetail.purchasePrice, tblPurchase.PurchaseDate
FROM tblPurchase INNER JOIN tblPurchaseDetail ON tblPurchase.PurchaseID = tblPurchaseDetail.PurchaseID;

Use that query in another:

SELECT Purchases_Details.*
FROM Purchases_Details WHERE PDID IN (SELECT TOP 1 PDID FROM Purchases_Details AS Dupe WHERE Dupe.ItemID=Purchases_Details.ItemID
ORDER BY Dupe.PurchaseDate DESC, Dupe.PDID DESC);
thank you so much @June7 ..its working perfect
 

Mike Krailo

Well-known member
Local time
Today, 14:52
Joined
Mar 28, 2020
Messages
1,044
It's always helpful to others when you post what you did exactly to fix your problem.
 

June7

AWF VIP
Local time
Today, 10:52
Joined
Mar 9, 2014
Messages
5,473
Could JOIN second query to table of products to show all even if never purchased.
 

FahadTiger

Member
Local time
Today, 21:52
Joined
Jun 20, 2021
Messages
115
Could JOIN second query to table of products to show all even if never purchased.
the user enter any new item from the combobox in the sub form and after update event I used function Dlookup to get me last price from the Query that you are did it for me..if new item i get value zero in the textbox of last price
 

Mike Krailo

Well-known member
Local time
Today, 14:52
Joined
Mar 28, 2020
Messages
1,044
I'm sorry FahadTiger, I was hoping you would actually post the working code that June7 helped you with. That makes it perfectly clear what your solution was. I'm glad you got things working the way you desire.
 

Users who are viewing this thread

Top Bottom