Results 1 to 4 of 4
  1. #1
    Join Date
    Mar 2022
    Posts
    4

    Match number in Table A with closest matching number in Table B

    At first glance I thought this would be simple but having a tough time figuring this out.
    I have a tblA_Amounts of Ingredients and Amounts (in ml) and a tblB_Scoops of Scoops & ScoopSize (in ml). For each ingredient amount I want to join on closest matching scoop from tbl_Scoops. E.g. for Blackberries I need about 170ml. I don't have a 170ml scoop but closest is a 150ml scoop.
    Click image for larger version. 

Name:	TableATableB.png 
Views:	17 
Size:	18.3 KB 
ID:	47581
    What query/SQL could I use to get closest matching scoop for all the Ingredients/Amounts on tblA_Amount? See my (manual) mock up qryDesiredResult...

    Click image for larger version. 

Name:	qryDesiredResult2.png 
Views:	16 
Size:	15.6 KB 
ID:	47583
    Attached Thumbnails Attached Thumbnails qryDesiredResult.png  

  2. #2
    ranman256's Avatar
    ranman256 is online now VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,527
    after you join the 2 tables, get the difference:

    Diff: ABS(A.Amt - B.Amt)

    sort ascending

    or you can use that query to get DMIN() to get the least different.

  3. #3
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,451
    to add to ranmans comments, sort ABS(A.Amt - B.Amt) ascending and use TOP 1 which might return two values if the difference is the same both ways. Say quantity is 175 - it would return both 150 and 200, so order by both difference (ascending) and scoop size (ascending if you want the smaller size, or descending if you want the larger size)

  4. #4
    Join Date
    Mar 2022
    Posts
    4
    Cheers for those ideas guys - I'm trying to put them into practice in an SQL statement. Sorry to say I'm not doing so well! Could you hammer a statement you think would work best?

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

Similar Threads

  1. Replies: 2
    Last Post: 03-29-2021, 11:26 AM
  2. Replies: 4
    Last Post: 02-26-2021, 05:58 PM
  3. Replies: 5
    Last Post: 02-24-2020, 08:29 AM
  4. Update table based on closest match on date
    By dalahans in forum Queries
    Replies: 6
    Last Post: 06-07-2018, 07:50 AM
  5. Record Number in Table and Form No Longer Match
    By amyrose1978 in forum Access
    Replies: 3
    Last Post: 09-21-2016, 08:05 AM

Tags for this Thread

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