• prathibha_aviator (10/15/2012)


    For the same data Provide the SQL statement that would return every CustomerName and the count of orders they have ever placed.

    I have tried using the below code

    select Customer.CustomerName, Count(CustomerOrders.customerID) from Customer JOIN CustomerOrders ON customer.customerID=customerorders.CustomerID

    giving an error:

    Msg 8120, Level 16, State 1, Line 1

    Column 'Customer.CustomerName' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.

    Read your error message, then read this.

    GROUP BY Customer.CustomerName

    Make sure you read the link I've provided to explain the issue.

    --EDIT--

    Seems Sean beat me to it.


    Forever trying to learn
    My blog - http://www.cadavre.co.uk/
    For better, quicker answers on T-SQL questions, click on the following...http://www.sqlservercentral.com/articles/Best+Practices/61537/
    For better, quicker answers on SQL Server performance related questions, click on the following...http://www.sqlservercentral.com/articles/SQLServerCentral/66909/