Results 1 to 3 of 3
  1. #1
    dipique is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jan 2012
    Posts
    31

    Query to join records with the closest values

    I have two tables, like this:

    tblInventory
    ID | Size | Add'l Fields...
    1 | 2
    2 | 4
    3 | 6
    4 | 7
    5 | 10

    tblRequests
    ID | ReqSize | Add'l Fields...


    1 | 2.5
    2 | 4
    3 | 7.25

    I want to write a query that will match up requests with the closest size--basically, where the record has the minimum ABS(Size-ReqSize). The ideal query would return:

    qryMatch
    ReqID | InvID
    1 | 1
    2 | 2
    3 | 4

    How can I write a query to join tables when the records don't match, they're just the closest match?

    Thanks!

    Dan

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    Why retrieve the ID? This will retrieve the Size.

    SELECT tblRequests.*, DMax("Size","tblInventory","Size<=" & [ReqSize]) AS NearestLower FROM tblRequests;
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    dipique is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jan 2012
    Posts
    31
    You are a brilliant, brilliant individual. Thank you. Marking as solved.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 4
    Last Post: 02-17-2014, 11:19 AM
  2. Adding records to a join table using a query
    By brharrii in forum Queries
    Replies: 5
    Last Post: 04-12-2013, 11:08 AM
  3. Trouble finding closest match
    By cutsygurl in forum SQL Server
    Replies: 1
    Last Post: 02-22-2013, 03:59 PM
  4. Replies: 1
    Last Post: 02-19-2012, 07:21 PM
  5. find the closest year
    By 12345678 in forum Queries
    Replies: 1
    Last Post: 05-25-2011, 05:07 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums