“您尝试执行不包含指定的聚合函数的查询" [英] "You tried to execute a query that does not include the specified aggregate function"

查看:98
本文介绍了“您尝试执行不包含指定的聚合函数的查询"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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列表中,但没有包含在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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆