• 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.

    The error message tells you EXACTLY what the problem is. You need to use some grouping when aggregating data.

    http://msdn.microsoft.com/en-us/library/ms177673%28v=sql.105%29.aspx

    --edit--

    I did not provide an answer because this scream of homework. If I provide you an answer you do not learn the topic.

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/