select
person.firstname,
person.lastname,
sum(order_item.pricecharged) as [sales]
from person cross join order_item
join ORDERS
on ORDERS.EmployeeID = PERSON.PersonID
group by person.firstname
收到错误:
Msg 8120,Level 16,State 1,Line 5 列'person.LastName'在选择列表中无效,因为它不包含在聚合函数或GROUP BY子句中。
(对SQL查询道歉仍然是新的)
答案 0 :(得分:2)
完全按照错误消息告诉您,放置所有"非聚合"列分组:
SELECT
person.firstname -- this is "non-aggregating"
, person.lastname -- this is "non-aggregating"
, SUM(order_item.pricecharged) AS [sales] -- this is "aggregating"
FROM person
CROSS JOIN order_item
JOIN ORDERS ON ORDERS.EmployeeID = PERSON.PersonID
GROUP BY
person.firstname
, person.lastname
答案 1 :(得分:2)
错误告诉您错误 - 您不能在选择列表中有一个列(特别是person.lastname),该列不在group by列表中,除非它上面有聚合函数。您是按名字分组,然后在输出中添加姓氏。如果你有鲍勃·琼斯和鲍勃·史密斯怎么办 - 两者的销售额将汇总在一起,但是你们没有告诉SQL如何处理这个姓氏,所以这是错误的。
在这种情况下,您可能希望将组行更改为
group by person.firstname, person.lastname