Results 1 to 12 of 12
  1. #1
    amywilson95 is offline Novice
    Windows Vista Access 2007
    Join Date
    Dec 2015
    Posts
    7

    Making queries from multiple tables

    I am trying to create a query using multiple tables.



    I have a master data table, which includes the codes of various locations.
    I have a few other tables which contain info for rent, rates etc

    I want to create a query that shows the sum of the rent, rates etc for each location code.
    It works with one table, but then when I add more tables the figures don't correspond with the original data?

    I have a relationship between the master data code and the codes in the other tables.

    Am I missing something? Can anyone assist?

  2. #2
    ranman256's Avatar
    ranman256 is online now VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,549
    You may want a UNION query. You query many different tables, ie:
    select clientID, amt from tRent
    union
    select clientID, rate from tRates

  3. #3
    orange's Avatar
    orange is offline Moderator
    Windows XP Access 2003
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,849
    Can you show us a jpg of your relationships window? Please expand all tables to show all fields before doing the jpg.

  4. #4
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,641
    Not a UNION query.

    Build aggregate (GROUP BY) Totals query for each table that needs to be summed. Then join those queries to the master locations table.

    Or build report/subreports arrangement.
    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.

  5. #5
    amywilson95 is offline Novice
    Windows Vista Access 2007
    Join Date
    Dec 2015
    Posts
    7
    I have tried doing it this way - seems to be bringing the results I want.

    BUT... it is only showing results for less than a quarter of the locations in my master data, any idea why??

    Thanks


    Quote Originally Posted by June7 View Post
    Not a UNION query.

    Build aggregate (GROUP BY) Totals query for each table that needs to be summed. Then join those queries to the master locations table.

    Or build report/subreports arrangement.

  6. #6
    orange's Avatar
    orange is offline Moderator
    Windows XP Access 2003
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,849

  7. #7
    amywilson95 is offline Novice
    Windows Vista Access 2007
    Join Date
    Dec 2015
    Posts
    7
    Here is the SQL...

    SELECT [Master Data].Code, Sum([Rent Q].[SumOfRent Amount]) AS [SumOfSumOfRent Amount], Sum([Rates Q].[SumOfRates Amount]) AS [SumOfSumOfRates Amount], Sum([FM Q].[SumOfFM Amount]) AS [SumOfSumOfFM Amount], Sum([Fixed Occupancy Q].[SumOfFixed Occupancy Amount]) AS [SumOfSumOfFixed Occupancy Amount], Sum([Variable Maintenance Q].[SumOfVariable Maintenance - Net Sales Value]) AS [SumOfSumOfVariable Maintenance - Net Sales Value]
    FROM (((([Master Data] INNER JOIN [Fixed Occupancy Q] ON [Master Data].Code = [Fixed Occupancy Q].Code) INNER JOIN [FM Q] ON [Master Data].Code = [FM Q].Code) INNER JOIN [Rates Q] ON [Master Data].Code = [Rates Q].Code) INNER JOIN [Rent Q] ON [Master Data].Code = [Rent Q].Code) INNER JOIN [Variable Maintenance Q] ON [Master Data].Code = [Variable Maintenance Q].Code
    GROUP BY [Master Data].Code;


    Quote Originally Posted by orange View Post
    Post the sql of your query.

  8. #8
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,641
    Don't use INNER JOIN, use OUTER - LEFT or RIGHT, probably RIGHT in this case - "All records from [Master Data] and only those from ..."
    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.

  9. #9
    amywilson95 is offline Novice
    Windows Vista Access 2007
    Join Date
    Dec 2015
    Posts
    7
    Sorry I'm confused, do I just replace everything that says "INNER JOIN" with "OUTER - RIGHT" ??
    SQL view is not something I've used before.


    Quote Originally Posted by June7 View Post
    Don't use INNER JOIN, use OUTER - LEFT or RIGHT, probably RIGHT in this case - "All records from [Master Data] and only those from ..."

  10. #10
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,641
    Use the query designer to help you construct the statement. Double click on the line that is linking between tables to open the Join Properties dialog.
    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.

  11. #11
    orange's Avatar
    orange is offline Moderator
    Windows 8 Access 2010 32bit
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,849
    Try some experimenting.
    Using Access, take 2 of your tables that are related.
    Create a query using an Inner Join, and see the result.
    Change the Inner Join to Left Join and see the query results.

    You can see specifics at http://www.w3schools.com/sql/sql_join_inner.asp
    .

  12. #12
    amywilson95 is offline Novice
    Windows Vista Access 2007
    Join Date
    Dec 2015
    Posts
    7
    Problem solved, thanks for your help everyone

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

Similar Threads

  1. Replies: 3
    Last Post: 02-02-2015, 11:32 AM
  2. Reports on multiple tables / queries
    By drnld in forum Access
    Replies: 5
    Last Post: 07-02-2014, 09:04 AM
  3. Replies: 3
    Last Post: 10-18-2013, 07:29 AM
  4. Queries with multiple tables to create report
    By Solstice in forum Queries
    Replies: 1
    Last Post: 09-22-2011, 02:23 PM
  5. Help making a form write to multiple tables
    By shiphtfour in forum Forms
    Replies: 2
    Last Post: 01-15-2011, 04:12 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