SQL Server 使用select [duplicate]分组

sbdsn5lh  于 2023-02-07  发布在  其他
关注(0)|答案(1)|浏览(156)
    • 此问题在此处已有答案**:

Reason for Column is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause [duplicate](4个答案)
2天前关闭。
我正在尝试解决KhanAcademy的"客户订单挑战",但不是在平台上,而是在SQL Management Studio中。我还没有获得与平台上相同的结果。
这是我的代码,当我尝试在SQL Server中运行它时出现错误。
"列" Customers. Name "在选择列表中无效,因为它既未包含在聚合函数中,也未包含在GROUP BY子句中。"
我真的需要你帮忙。

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
ryevplcw

ryevplcw1#

除了聚集之外,您SELECT的任何列都应放在GROUP BY子句中

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

相关问题