Results 1 to 3 of 3
  1. #1
    rustynails is offline Novice
    Windows 8 Access 2007
    Join Date
    Jul 2013
    Posts
    25

    Compare two tables and select records based on unmatch rules

    It's a pretty straightforward requirement. We have two tables, A and B. Both of them have one field called Hours. I have to select Hours from either A or B depending on few criteria.

    Click image for larger version. 

Name:	values.JPG 
Views:	3 
Size:	17.7 KB 
ID:	13400

    The criteria is as follows:

    If A.Hours = B.Hours, reject records
    If A.Hours <> B.Hours, select A.Hours
    If A.Hours <> B.Hours, select NEXT B.Hours where B.Hours > A.Hours


    If A.Hours <> B.Hours AND B.Hours = A.Hours+1, reject records

    Based on the attachment I'm providing, I want the result set to look like this (except the grey fields which I added for clarity):

    Click image for larger version. 

Name:	hours.JPG 
Views:	3 
Size:	16.4 KB 
ID:	13399

    I'm also attaching Access 2000 mdb file. I dont want to do this all in one query...if it helps to break the query into 3 or 4 different pieces, then I'm all for it. Any help is super appreciated
    Attached Files Attached Files

  2. #2
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,930
    Query1:
    SELECT A.Hours, DMin("Hours","B","Hours>" & [Hours]) AS BgreaterA FROM A
    WHERE (((DLookUp("Hours","B","Hours=" & [Hours])) Is Null) AND ((DLookUp("Hours","B","Hours=" & [Hours]+1)) Is Null));

    Query2:
    SELECT Val(Hours) As Hrs FROM Query1
    UNION SELECT Val(BgreaterA) FROM Query1;
    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
    rustynails is offline Novice
    Windows 8 Access 2007
    Join Date
    Jul 2013
    Posts
    25

    Talking

    Quote Originally Posted by June7 View Post
    Query1:
    SELECT A.Hours, DMin("Hours","B","Hours>" & [Hours]) AS BgreaterA FROM A
    WHERE (((DLookUp("Hours","B","Hours=" & [Hours])) Is Null) AND ((DLookUp("Hours","B","Hours=" & [Hours]+1)) Is Null));

    Query2:
    SELECT Val(Hours) As Hrs FROM Query1
    UNION SELECT Val(BgreaterA) FROM Query1;
    You are amazing June7. Thank you

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

Similar Threads

  1. Replies: 12
    Last Post: 04-25-2013, 01:32 PM
  2. Replies: 1
    Last Post: 07-11-2011, 05:12 AM
  3. Access compare and sum records in two tables
    By piszczel in forum Queries
    Replies: 8
    Last Post: 05-23-2011, 02:07 AM
  4. Compare records in 2 tables
    By RalphJ in forum Access
    Replies: 13
    Last Post: 03-06-2011, 07:43 AM
  5. Select Random Records Based on Conditions
    By access123 in forum Queries
    Replies: 1
    Last Post: 10-27-2010, 10:25 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