“您试图执行不包含指定聚合函数的查询" [英] "You tried to execute a query that does not include the specified aggregate function"
问题描述
SELECT SUM(orders.quantity) AS num, fName, surname
FROM author
INNER JOIN book ON author.aID = book.authorID;
我不断收到错误消息:您尝试执行的查询不包含指定的表达式fName"作为聚合函数的一部分.我该怎么办?
I keep getting the error message: "you tried to execute a query that does not include the specified expression "fName" as part of an aggregate function. What do I do?
推荐答案
该错误是因为 fName
包含在 SELECT
列表中,但未包含在 fName
列表中code>GROUP BY 子句并且不是聚合函数的一部分 (Count()
, Min()
, Max()
、Sum()
等)
The error is because fName
is included in the SELECT
list, but is not included in a GROUP BY
clause and is not part of an aggregate function (Count()
, Min()
, Max()
, Sum()
, etc.)
您可以通过在 GROUP BY
中包含 fName
来解决该问题.但是随后您将面临与 surname
相同的问题.所以把它们都放在 GROUP BY
中:
You can fix that problem by including fName
in a GROUP BY
. But then you will face the same issue with surname
. So put both in the GROUP BY
:
SELECT
fName,
surname,
Count(*) AS num_rows
FROM
author
INNER JOIN book
ON author.aID = book.authorID;
GROUP BY
fName,
surname
注意我在你想要SUM(orders.quantity)
的地方使用了Count(*)
.但是,orders
不包含在查询的 FROM
部分中,因此您必须先包含它,然后才能 Sum()
字段.
Note I used Count(*)
where you wanted SUM(orders.quantity)
. However, orders
isn't included in the FROM
section of your query, so you must include it before you can Sum()
one of its fields.
如果您有可用的 Access,请在查询设计器中构建查询.它可以帮助您了解可以使用哪些功能并应用正确的 Access SQL 语法.
If you have Access available, build the query in the query designer. It can help you understand what features are possible and apply the correct Access SQL syntax.
这篇关于“您试图执行不包含指定聚合函数的查询"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!