Results 1 to 8 of 8
  1. #1
    rspai is offline Novice
    Windows XP Access 2007
    Join Date
    May 2012
    Posts
    3

    Joining two tables

    There are two tables: GLEntries and RepaymentSchedule
    Each of them has the same structure: Account and Amount


    However, the GL Entries table has less rows than the RepaymentSchedule table.


    When the following query is run it returns 157 rows:


    Select GLEntries.Account, GLEntries.Amount, RepaymentSchedule.Amount
    from GLEntries RIGHT JOIN RepaymentSchedule
    ON GLEntries.Account = RepaymentSchedule.Account
    where RepaymentSchedule.Amount > GLEntries.Amount


    I tried changing "RIGHT JOIN" to "INNER JOIN" AND "LEFT JOIN". The other queries too return 157 rows.


    How should be the query be reworded to get all rows from he RepaymentSchedule table where there are no equivalent Accounts in the GL Entries table?


    Thanks in advance for the help.

  2. #2
    jzwp11 is offline VIP
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jun 2010
    Location
    Dayton, OH
    Posts
    2,901
    How should be the query be reworded to get all rows from he RepaymentSchedule table where there are no equivalent Accounts in the GL Entries table?
    You will actually need to use the "Find Unmatched Query Wizard" to create the query you describe.

    It will look like this:

    SELECT RepaymentSchedule.Account, RepaymentSchedule.Amount
    FROM RepaymentSchedule LEFT JOIN GLEntries ON RepaymentSchedule.[Account] = GLEntries.[Account]
    WHERE (((GLEntries.Account) Is Null));

    But, since both tables have the same structure, by normalization rules, you should have only 1 table. You can add a field to designated a GL entry or a repayment entry.

  3. #3
    ssanfu is offline Master of Nothing
    Windows 2K Access 2000
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    The SQL looks right to me. Try removing the WHERE clause to see if all records from the RepaymentSchedule table are returned. Maybe add an ORDER BY clause on Account number
    Code:
    Select GLEntries.Account, GLEntries.Amount, RepaymentSchedule.Amount 
    from GLEntries  RIGHT JOIN RepaymentSchedule 
    ON GLEntries.Account = RepaymentSchedule.Account
    ORDER BY RepaymentSchedule.Account

  4. #4
    Robeen is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    Mar 2011
    Location
    Tulsa, Oklahoma.
    Posts
    1,596
    Try this:
    Code:
    Select * From RepaymentSchedule Where NOT EXISTS
    (Select GLEntries.Account 
    From GLEntries
    Where GLEntries.Account = RepaymentSchedule.Account);

  5. #5
    rspai is offline Novice
    Windows XP Access 2007
    Join Date
    May 2012
    Posts
    3
    Maybe I have not stated the issue correctly in my post above. Although the structures of the two tables are the same, the amounts in the amount column vary. ("where RepaymentSchedule.Amount > GLEntries.Amount")

    Hence the query results should return all accounts where the amount in the RepaymentSchedule table are greater than than the amounts in the GLEntries table
    AND
    all accounts in the RepaymentSchedule table which are not present in the GLEntries table

  6. #6
    ssanfu is offline Master of Nothing
    Windows 2K Access 2000
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    In your first post, you said:
    However, the GL Entries table has less rows than the RepaymentSchedule table.

    When the following query is run it returns 157 rows:
    So how many records did you expect?
    How many records are in GL Entries table?
    How many records are in the RepaymentSchedule table?

    I read your post one way. jzwp11 read it another. And Robeen read it still another way.

    Hence the query results should return all accounts where the amount in the RepaymentSchedule table are greater than than the amounts in the GLEntries table
    AND
    all accounts in the RepaymentSchedule table which are not present in the GLEntries table
    Not sure it can be done in one query. It might have to be a union query which is not updatable. What is this to be used for? If the record source for a report, a union query would be acceptable. Not so much for a form...

    You might try the following... it is entirely a WAG!!! Probably won't give the correct results, but...:
    Code:
    Select GLEntries.Account, GLEntries.Amount, RepaymentSchedule.Amount 
    from GLEntries  RIGHT JOIN RepaymentSchedule 
    ON GLEntries.Account = RepaymentSchedule.Account 
    WHERE (RepaymentSchedule.Amount > GLEntries.Amount) OR ((GLEntries.Account) IS NULL)

  7. #7
    rspai is offline Novice
    Windows XP Access 2007
    Join Date
    May 2012
    Posts
    3
    Thanks a ton, Steve.

    That was the query I was looking for. I tweaked it it a bit to get the Account numbers from the RepaymentSchedule table. (Earlier, it was returning blanks for account numbers because of "GLEntries.Account"

    Select RepaymentSchedule.Account, GLEntries.Amount, RepaymentSchedule.Amount
    from GLEntries RIGHT JOIN RepaymentSchedule
    ON GLEntries.Account = RepaymentSchedule.Account
    WHERE (RepaymentSchedule.Amount > GLEntries.Amount) OR ((GLEntries.Account) IS NULL)

    The query is used for a report.

  8. #8
    ssanfu is offline Master of Nothing
    Windows 2K Access 2000
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    Wonderful

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

Similar Threads

  1. Joining 2-Tables in a query
    By djclntn in forum Queries
    Replies: 7
    Last Post: 03-15-2012, 08:37 AM
  2. Options when joining two tables
    By Kevin_ in forum Queries
    Replies: 9
    Last Post: 07-29-2011, 10:06 PM
  3. joining tables
    By frozendmj in forum Queries
    Replies: 5
    Last Post: 04-19-2011, 01:45 PM
  4. Joining more than 2 tables
    By anemoskkk in forum Access
    Replies: 6
    Last Post: 04-17-2011, 04:37 AM
  5. Help - Joining Tables..
    By fabian24 in forum Forms
    Replies: 3
    Last Post: 02-17-2011, 03:43 PM

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