I'm using access to query our main database. I'm linking 2 tables together,
the Vendor table and the Vendor Part table.
The Vendor part table contains the part number and any multiple of cost
records so you are correct that there can be more than 2 records for each
part number. However, I'm only interested in the latest effective date and
the previous effective date.
I can not do anything with the tables. I can only query them.
My goal is to be able to have a query that gives me each part number and the
price DIFFERENCE between the most current effective date and the effective
date before it.
You should be able to use a Subquery to search the table of costs. Something
like:
SELECT PUB_Vendor.Company, PUB_Vendor.VendorID, PUB_VendPart.PartNum,
PUB_VendPart.EffectiveDate, PUB_VendPart.BaseUnitPrice,
PUB_VendPart.VenPartNum, PUB_VendPart.BaseUnitPrice - (SELECT TOP 1
BaseUnitPrice FROM PUB_VendPart AS Y WHERE Y.PartNum = PUB_VendPart.PartNum
AND Y.EffectiveDate < PUB_VendPart.EffectiveDate)
FROM PUB_Vendor INNER JOIN PUB_VendPart ON (PUB_Vendor.VendorNum =
PUB_VendPart.VendorNum) AND (PUB_Vendor.Company = PUB_VendPart.Company)
WHERE (((PUB_Vendor.Company)="Loc") AND ((PUB_Vendor.VendorID)="M702") AND
EffectiveDate = (SELECT Max(EffectiveDate) FROM PUB_VendPart AS X WHERE
X.PartNum = Pub_VendPart.PartNum)
ORDER BY PUB_VendPart.PartNum;
This is untested air code, and assumes that there always *is* a previous
price. You may want to add an NZ() function to handle cases where there isn't.