Results 1 to 5 of 5
  1. #1
    Wallatrix is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jan 2013
    Posts
    3

    Exclamation Tablename could refer to more than one table listed in the FROM clause.


    Hi,

    I am taking a database concepts class and I cannot, for the life of me, figure out how to effectively code SQL queries. Every time I run a query I get some sort of error message. I've read other forums on this site about how to fix this issue that I am having, but nothing seems to work. Yes, I have qualified my fields and yes I have every table containing the specific tablename listed... but nothing seems to work. Here is my source code, I hope someone can help me.

    Code:
    SELECT Customer.CustomerName, Orders.OrderNum, OrderLine.OrderNum, SUM(NumOrdered * QuotedPrice)
             FROM Customer, OrderLine, Orders
             WHERE Customer.CustomerNum = Orders.CustomerNum
             AND Orders.OrderNum = OrderLine.OrderNum
             GROUP By CustomerName, OrderNum
    When I try to run this query I get an error message that states "The specified field 'OrderNum' could refer to more than one table listed in the FROM clause of your SQL statement." Even though I have qualified my fields and there are only 2 tables (Orders and OrderLine) that has a OrderNum column. Please help me!

  2. #2
    JoeM is offline VIP
    Windows XP Access 2007
    Join Date
    Jun 2012
    Posts
    3,904
    You have listed the OrderNum field twice in your select statement (one from each table), and you haven't qualified this field at all in your "GROUP BY" statement.
    Since you are only returning records where the OrderNum between the Orders and OrderLine tables are the same, there is no need to return it twice (once from each field) in your Select statement.

    It is also better form to define your relationships in your FROM clause than to do it in the WHERE clause, i.e.
    Code:
    SELECT Customer.CustomerName, Orders.OrderNum, SUM(NumOrdered * QuotedPrice)
             FROM Customer, 
             INNER JOIN Orders
             ON Customer.CustomerNum = Orders.CustomerNum
             INNER JOIN OrderLine
             ON Orders.OrderNum = OrderLine.OrderNum
             GROUP By Customer.CustomerName, Orders.OrderNum

  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,848
    Since OrderNum is a field in both Orders and Orderline tables, you must include the table reference in your Group BY statement to explicitly tell Access/Jet what you mean. (Also called disambiguate)


    GROUP By CustomerName, Orders.OrderNum
    Or
    GROUP By CustomerName, Orderline.OrderNum


    OOps: I see JoeM has responded while I was fighting with a bad internet connection.

  4. #4
    Wallatrix is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jan 2013
    Posts
    3
    Quote Originally Posted by JoeM View Post
    You have listed the OrderNum field twice in your select statement (one from each table), and you haven't qualified this field at all in your "GROUP BY" statement.
    Since you are only returning records where the OrderNum between the Orders and OrderLine tables are the same, there is no need to return it twice (once from each field) in your Select statement.

    It is also better form to define your relationships in your FROM clause than to do it in the WHERE clause, i.e.
    Code:
    SELECT Customer.CustomerName, Orders.OrderNum, SUM(NumOrdered * QuotedPrice)
             FROM Customer, 
             INNER JOIN Orders
             ON Customer.CustomerNum = Orders.CustomerNum
             INNER JOIN OrderLine
             ON Orders.OrderNum = OrderLine.OrderNum
             GROUP By Customer.CustomerName, Orders.OrderNum

    Thank you for helping me out. I didn't think of that solution; too much cobol and java in my head. My teacher wants us to start out using the WHERE clause for joining tables until further on in the class.

  5. #5
    JoeM is offline VIP
    Windows XP Access 2007
    Join Date
    Jun 2012
    Posts
    3,904
    My teacher wants us to start out using the WHERE clause for joining tables until further on in the class.
    I think I just lost a lot of respect for your teacher!

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

Similar Threads

  1. Replies: 4
    Last Post: 02-16-2012, 05:23 PM
  2. Replies: 1
    Last Post: 10-11-2011, 11:16 AM
  3. Replies: 10
    Last Post: 08-08-2011, 01:55 PM
  4. Field "F1" does not exist in table (Tablename)
    By HelenP in forum Import/Export Data
    Replies: 3
    Last Post: 11-26-2010, 09:13 AM
  5. Replies: 4
    Last Post: 03-12-2010, 05:42 PM

Tags for this Thread

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