Results 1 to 12 of 12
  1. #1
    Kaisa is offline Novice
    Windows Vista Access 2003
    Join Date
    Jul 2011
    Posts
    7

    Question Problem with Natural Join

    Please help!!!

    I am not a database developer although I've always thought I was pretty good for a non-specialist. I guess, I was wrong.


    I am trying to join two queries.

    I am not sure what I am doing wrong, seems like such a trivial command but I keep getting a syntax error message.

    My query is:

    SELECT *
    FROM 023_Option_Total_2018 NATURAL JOIN 023_Option_Total_2025;


    These are basically two queries with absolutely similar structure, except one goes from 2011 to 2018 and the other from 2019 to 2025




    Why is my "From" wrong? what am I missing?

  2. #2
    jzwp11 is offline VIP
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jun 2010
    Location
    Dayton, OH
    Posts
    2,901
    If you are using Access "NATURAL JOIN" is not valid syntax. If the tables are identical in structure that tells me that there should be only 1 table. You can add an additional field to distinguish records of the two time periods. You would then use a query to pull the records you want.

  3. #3
    Kaisa is offline Novice
    Windows Vista Access 2003
    Join Date
    Jul 2011
    Posts
    7
    Yes, they are identical but I had reason for separating them into two.

    These queries are based on few other queries with excessive calculations and since each of them have around 100 columns, Access refused to calculate stuff in one query (too complex).

    I am sure there is a much better way to deal with this issue but like I said, I hope I am pretty skilled for an accountant

    Could you please let me know what the correct syntax in Access is?

  4. #4
    jzwp11 is offline VIP
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jun 2010
    Location
    Dayton, OH
    Posts
    2,901
    Technically speaking having two tables with the same structure violates normalization rules, so I would recommend merging the two tables at some point.

    But to answer your immediate question, the only other way to do it is to bring the data from the two tables/queries together using a UNION query. This has to be done in SQL view. The order and number of fields in the two SELECT clauses must be the same.

    SELECT field1, field2 ...
    FROM table/query1

    UNION

    Select field1, field2...
    FROM table/query2

  5. #5
    Kaisa is offline Novice
    Windows Vista Access 2003
    Join Date
    Jul 2011
    Posts
    7
    This is actually the point when I try to merge them - all the calclulations are done.

    Because I was not able to solve the merging problem, what I do is copy the data into excel but I thought it would be nice to be able to join them if I could.

    I've used union query but only to add the rows, let me see if this will work with the columns as well.

  6. #6
    Kaisa is offline Novice
    Windows Vista Access 2003
    Join Date
    Jul 2011
    Posts
    7
    It did not work

    I have two tables (queries)

    1) ID 2011 2012 (10 rows)
    2) ID 2013 2014 (10 rows)

    and I want to get

    ID 2011 2012 2013 2014 (10 rows)

    What I got was

    ID 2011 2012 (20 rows)



    Thanks anyways

  7. #7
    jzwp11 is offline VIP
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jun 2010
    Location
    Dayton, OH
    Posts
    2,901
    So you want to keep the fields distinct? Ok, sorry I misinterpreted what you were after. It sounds like you need a result that is the Cartesian Product between the two tables, if so try this

    SELECT query1.*, query2.*
    FROM query1, query2

  8. #8
    Kaisa is offline Novice
    Windows Vista Access 2003
    Join Date
    Jul 2011
    Posts
    7
    I tried this (in design view) too but then I got problems with duplications becuase technically the two queries dont have any ID's that the records cound be identified with - I just have three fields that I the same in both. but when I join those, I get more combinations or something.

    I guess, I better think of a way to introduce some kind of unique id for each record, right?

  9. #9
    jzwp11 is offline VIP
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jun 2010
    Location
    Dayton, OH
    Posts
    2,901
    It sounds like you need a foreign key in each record that relates back to something else. Unfortunately, I do not know what that something else is. Typically, you might have something like this:

    tblCustomer
    -pkCustID primary key, autonumber
    -txtCustomerName

    table1
    -ID
    -2011
    -2012
    -fkCustomerID foreign key to tblCustomer

    table2
    -ID
    -2013
    -2014
    -fkCustomerID foreign key to tblCustomer

    You would then construct the query with the three tables joining via the customerID field

  10. #10
    Kaisa is offline Novice
    Windows Vista Access 2003
    Join Date
    Jul 2011
    Posts
    7
    Thank you very much for everything, I think I got an idea how to fix everything.

  11. #11
    jzwp11 is offline VIP
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jun 2010
    Location
    Dayton, OH
    Posts
    2,901
    Sorry that I could not be of more help.

  12. #12
    Kaisa is offline Novice
    Windows Vista Access 2003
    Join Date
    Jul 2011
    Posts
    7
    Quote Originally Posted by jzwp11 View Post
    Sorry that I could not be of more help.
    You helped a lot!

    Thank you very much.

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

Similar Threads

  1. Nested join problem
    By kman42 in forum Queries
    Replies: 2
    Last Post: 03-25-2011, 02:05 PM
  2. Left join problem
    By e.badin in forum Queries
    Replies: 5
    Last Post: 01-17-2011, 08:03 AM
  3. Problem with Join
    By sujitshukla in forum Queries
    Replies: 1
    Last Post: 08-26-2010, 07:25 AM
  4. Many to many self-join problem
    By dbdbdo in forum Database Design
    Replies: 1
    Last Post: 07-18-2010, 09:31 AM
  5. Problem with Left Join & VB6
    By msrdjan1 in forum Queries
    Replies: 0
    Last Post: 03-30-2010, 01:48 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