Results 1 to 9 of 9
  1. #1
    pinecrest515 is offline Advanced Beginner
    Windows Vista Access 2007
    Join Date
    Dec 2010
    Posts
    31

    Interesting and Urgent Join Query Question

    Dear forumers,

    I would like to consult urgently on the following:

    Suppose I have two tables in Access, and want to join them:

    Table 1:
    Company This_Year_Sales
    Disney 1000
    Pepsi 2000


    Table 2:


    Company Last_Year_Sales
    Coke 3000
    Target 1000

    I want to join these tables together so that all the information is presented, with three columns: Company, This_Year_Sales, Last_Year_Sales, like this:

    Company This_Year_Sales Last_Year_Sales
    Disney 1000 n/a
    Pepsi 2000 n/a
    Coke n/a 3000
    Target n/a 1000

    How do I do it?

  2. #2
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Presuming you have a table with all possible companies, you'd use a LEFT JOIN against that table:

    FROM CompaniesTable LEFT JOIN Table1 ON CompaniesTable.Company = Table.Company

    You can add the other once you see how that one looks in design view. You can use the Nz() function to replace the Null value when a company doesn't have a record in Table1 with "n/a".
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    pinecrest515 is offline Advanced Beginner
    Windows Vista Access 2007
    Join Date
    Dec 2010
    Posts
    31
    The problem is that I don't have an actual master table for company. Suppose the company names listed in Table1 and Table2 are different. Table1 maps companies to last year sales, Table2 maps companies to this year sales. I want a joined table listing all companies in Tables 1 and 2, with two columns, one for last year sales and the other for this year sales. It's ok if some companies don't have last year sales and some companies don't have this year sales.

    * please keep in mind that the table and data is fake, a very simpified mock version of the real tables.

  4. #4
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    In place of a master table, you can use a UNION query to get a list of distinct companies from the two tables.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  5. #5
    pinecrest515 is offline Advanced Beginner
    Windows Vista Access 2007
    Join Date
    Dec 2010
    Posts
    31
    I tried the following UNION ALL query. However, I am getting an error from Access saying I cannot Union inside a sub-query. Help!!

    SELECT
    company,
    last_year_sales,
    this_year_sales
    FROM
    (
    SELECT
    company, 0 as last_year_sales, this_year_sales FROM
    Table1
    UNION ALL
    SELECT
    company, last_year_sales, 0 as this_year_sales FROM
    Table2) x
    GROUP BY
    company, last_year_sales, this_year_sales

  6. #6
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    That's actually not the way I was thinking of going, but it would work. Let the UNION query stand alone, then make a second query a totals query that groups on company and sums the other two fields. That will pull any companies with data in both tables back into one record.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  7. #7
    pinecrest515 is offline Advanced Beginner
    Windows Vista Access 2007
    Join Date
    Dec 2010
    Posts
    31
    I tried just doing the UNION/UNION ALL first, but got an error saying SELECT statement includes an argument name that is missing or mispelled.

    SELECT
    company, 0 as last_year_sales, this_year_sales FROM
    Table1
    UNION ALL
    SELECT
    company, last_year_sales, 0 as this_year_sales FROM
    Table2

    SELECT
    company, 0 as last_year_sales, this_year_sales FROM
    Table1
    UNION
    SELECT
    company, last_year_sales, 0 as this_year_sales FROM
    Table2

  8. #8
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Not all together, right? I'd expect this to work:

    SELECT company, 0 as last_year_sales, this_year_sales FROM
    Table1
    UNION ALL
    SELECT company, last_year_sales, 0 as this_year_sales FROM
    Table2

    Are those your actual field names? If not, you might try bracketing them, in case you have a reserved word in there. Can you post the db, or a representative sample?
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  9. #9
    weekend00 is offline I may not be right
    Windows XP Access 2003
    Join Date
    Aug 2010
    Posts
    1,295
    Besides all above replies, you can try another UNION query like this:

    select table1.company,this_year_sales,last_year_sales from table1 left join table2 on table1.company=table2.company
    union
    select table2.company,this_year_sales,last_year_sales from table1 right join table2 on table1.company=table2.company where table1.company is null

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

Similar Threads

  1. Help! Very interesting query problem
    By pinecrest515 in forum Queries
    Replies: 5
    Last Post: 12-15-2010, 11:46 AM
  2. Help with Query!!! (Interesting Problem)
    By pinecrest515 in forum Queries
    Replies: 4
    Last Post: 12-09-2010, 08:20 AM
  3. Help!!! Urgent Access SQL Query Question
    By pinecrest515 in forum Queries
    Replies: 1
    Last Post: 12-06-2010, 02:40 PM
  4. Interesting Query Problem
    By Lockrin in forum Queries
    Replies: 7
    Last Post: 08-23-2010, 01:56 PM
  5. Basic question, urgent help needed.
    By fishnu in forum Access
    Replies: 12
    Last Post: 03-18-2009, 01:39 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