-1

I'm trying to solve the "Customer's orders Challenge" from KhanAcademy, not in the platform but in SQL Management Studio. And I haven't obtain the same result as on the platform.

This is the challenge: "Step 2 Now, create another query that will result in one row per each customer, with their name, email, and total amount of money they've spent on orders. Sort the rows according to the total money spent, from the most spent to the least spent."

This is my code and the error that appears when I try to run it in SQL Server.

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

I really need a hand on this.

SELECT Customers.Name, Customers.email, SUM(Orders.Price) AS 'Total Orders'
    FROM Customers
    LEFT OUTER JOIN Orders
        ON Customers.ID = Orders.CustomerID
    GROUP BY Customers.ID
    ORDER BY Orders.Price DESC

1 Answers1

1

Any column that you SELECT besides aggregate should be placed in the GROUP BY clause

SELECT Customers.Name, Customers.email, SUM(Orders.Price) AS 'Total Orders'
FROM Customers
LEFT OUTER JOIN Orders
    ON Customers.ID = Orders.CustomerID
GROUP BY Customers.Name, Customers.email
Dale K
  • 25,246
  • 15
  • 42
  • 71
user2065377
  • 448
  • 3
  • 12
  • Sorry, but doing that, this is the results I get... Doctor Who doctorwho@timelords.com 1000000 Doctor Who doctorwho@timelords.com 1000 Harry Potter harry@potter.com 40 Captain Awesome captain@awesome.com NULL And what I really want is to get one row per each customer, with their name, email, and total amount of money they've spent on orders. – user21057499 Feb 07 '23 at 08:06