' FIELDNAME'不属于聚合函数 - Access SQL

时间:2015-07-13 18:35:08

标签: sql ms-access

我正在尝试从我的数据库中选择所有this is the top line of my string\nthis is the second line of the string 并获取有关每个[Operation]的数据。下面是我尝试使用的SQL代码。

发生以下错误:

You tried to execute a query that does not include the specified expression 'op' as part of an aggregate function

我的SQL声明:

SELECT [op] as [Operation], Sum(count) as [Number of Breakdowns], Sum(td) as [Sum of Time Down]
FROM
(
  SELECT [Operation] as op, Count(*) as count, Sum([Time Down]) as td
  FROM tblDailyDowntimeAssy
  WHERE [Type of Maintenance] = 'Breakdown'
UNION ALL
  SELECT [Operation] as op, Count(*) as count, Sum([Time Down]) as td
  FROM tblDailyDowntimeMach
  WHERE [Type of Maintenance] = 'Breakdown'
);

注意:

如果我尝试仅使用[Operation]字段并且不重命名op,则错误仍然会发生,但只是更改“操作”。到'操作'

2 个答案:

答案 0 :(得分:2)

  SELECT [Operation], Sum(count) as [Number of Breakdowns], Sum(td) as [Sum of Time Down]
FROM
(
  SELECT [Operation], Count(*) as count, Sum([Time Down]) as td
  FROM tblDailyDowntimeAssy
  WHERE [Type of Maintenance] = 'Breakdown'
  group by [Operation]
UNION ALL
  SELECT [Operation], Count(*) as count, Sum([Time Down]) as td
  FROM tblDailyDowntimeMach
  WHERE [Type of Maintenance] = 'Breakdown'
  group by [Operation]
)
group by Operation;

当您使用聚合函数时,需要在查询中添加GROUP BY子句。

答案 1 :(得分:0)

您需要在查询中添加group by子句

select ..... from tbl_name
group by ....

然后将该字段添加到错误

中提到的group by子句