Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

A simple sql query problem

Options
  • 31-03-2013 4:46pm
    #1
    Registered Users Posts: 2,475 ✭✭✭


    Hey guys and gals, just wondering if you can give me a hand with this query , it's driving me around the bend,

    Query
    Create a query to display the total sales price of all orders from each customer (two table involved)

    here is my query code

    SELECT lname,fname,cust_nbr,
    SUM(sale_price)
    From customer JOIN cust_order on cust_nbr=cust_nbr
    GROUP BY lname;

    I get this error, ORA-00918: column ambiguously defined

    when I run these separate query's they work no bother

    SELECT cust_nbr,SUM(sale_price)
    FROM cust_order
    GROUP BY cust_nbr;

    SELECT lname,fname
    FROM customer;

    this has been driving me nuts for a couple of hours and I'm sure it's just something simple I'm missing in the join, any help would appreciated.
    Regards from a noob


Comments

  • Registered Users Posts: 586 ✭✭✭Aswerty


    Try changing this line

    From customer JOIN cust_order on cust_nbr=cust_nbr

    to

    From customer JOIN cust_order on customer.cust_nbr=cust_order.cust_nbr

    Now you are specifying the table since the columns in both tables are the same name.


  • Registered Users Posts: 2,475 ✭✭✭bennyineire


    Thanks for the reply, tried that but I'm still getting the same error


  • Registered Users Posts: 1,206 ✭✭✭jordata


    Try changing your select clause to
    SELECT lname,fname,customer.cust_nbr, SUM(sale_price)


    As cust_nbr appears in both tables??


  • Registered Users Posts: 7,501 ✭✭✭BrokenArrows


    You need to name the tables you are using and place that name infront of the columns you want from that table.
    It is required incase there are columns in both tables with the same name.
    SELECT [B]cust.[/B]lname,[B]cust.[/B]fname,[B]cust_ord.[/B]cust_nbr,
     SUM([B]cust_ord.[/B]sale_price)
     From customer [B]cust[/B] 
          JOIN cust_order [B]cust_ord[/B] 
              on [B]cust.[/B]cust_nbr=[B]cust_ord.[/B]cust_nbr
     GROUP BY [B]cust.[/B]lname,[B]cust.[/B]fname,[B]cust_ord.[/B]cust_nbr;
    

    Edit. Also you need to include all columns which are not summed in the group by.


  • Registered Users Posts: 2,475 ✭✭✭bennyineire


    Yep that sorted , thanks a bunch guys, I see where I was going wrong


  • Advertisement
Advertisement