DataBase - Join based on next closest value (like Excel VLOOKUP)

Asked By Domenick
21-Nov-09 10:52 AM
Trying to do something similar to a VLOOKUP (Excel) in an Access 2003 query.
I have the following tables:

JOBS
Job,Quantity
A,96
B,256
C,300
D,4299

COSTS
Quantity,Cost
0, $1000
100, $1200
200, $1500
300, $2000
400, $2500
500, $3000

I need a query that takes JOBS.Quantity, looks it up in COSTS.Quantity and
find the cost for the NEXT LOWEST quantity.  (Example: Job B has a quantity
of 256 and the next LOWEST quantity from COSTS is 200 so Job B costs $1500.)

The results should be as follows:

JOBS.Job,COSTS.Cost
A,$1000
B,$1500
C,$2000
D,$3000

This would be easy to do in Excel with VLOOKUP. I'd like to do this with a
SQL query. I believe it can be done with non-equijoins and/or some grouping
with MAX or MIN. Can anyone offer a suggestion? I saw some stuff on the web
about using custom functions so I will also take suggestions on that method,
but I'd prefer to do it with straight query.

Thanks!
COSTS.Quantity
(1)
JOBS.Quantity
(1)
Access 2003
(1)
Excel
(1)
Sheridan
(1)
Stafford
(1)
Equijoins
(1)
MAX
(1)
  KenSheridan via AccessMonster.com replied to Domenick
21-Nov-09 12:04 PM
Assuming that cost increases with increased quantity:

SELECT Job, MAX(Cost)
FROM Jobs INNER JOIN Costs
ON Jobs.Quantity >= Costs.Quantity
GROUP BY Job;

Ken Sheridan
Stafford, England


--
Message posted via AccessMonster.com
http://www.accessmonster.com/Uwe/Forums.aspx/access-queries/200911/1
help
deployment issues of Access XP, Access 2003, and Access 2003 DataBase On my advice, a long time ago, a customer purchased the Access XP developer edition. That special Office XP version of Access was supposed to allow the Access developers to deploy Access XP applications within their organizations so that they did not have to purchase retail Access (or Office Professional) for every workstation that used the Access applications. Unfortunately, the deployment of
Access 2007 on Network DataBase I am trying to access Access 2007 from another computer. I get the "cannot lock file" error and cannot get access to the database. I have Access 2003 on my computer. What should I do? Access Setup Config Discussions Access 2003 (1 Access 2007 (1) Vista (1) Database (1) LauraS (1) Bontrager (1) Are you trying to open
access DataBase access Access Discussions Database (1) You now have access to the free peer-support technical newsgroup. If you have a question about a Microsoft Access database, please ask it; we are people, not a search engine. - - John W. Vinson [MVP] keywords: access description: access