执行不包含指定聚合函数错误消息的查询

时间:2014-03-05 19:02:23

标签: ms-access

我有以下查询返回错误消息"you tried to execute a query that does not include the specified expression"

SELECT o.ea_guid as CLASSGUID, o.object_type as CLASSTYPE, o.name, COUNT(o.name) as NameCount
FROM t_object as o
GROUP BY o.name
HAVING (COUNT(o.name)>1)

现在,如果我包括o.ea_guid& GROUP BY子句中的o.object_type,不返回任何结果

我做错了什么?

2 个答案:

答案 0 :(得分:2)

COUNT(o.name)是一个聚合;其他属性不是。如果要显示非聚合,则必须将其放在GROUP BY子句中。这将为您提供CLASSGUID,CLASSTYPE和NAME三个字段的每个唯一组合的行数。

SELECT 
    o.ea_guid as CLASSGUID, 
    o.object_type as CLASSTYPE,
    o.name as NAME, 
    COUNT(*) as NameCount
FROM 
    t_object as o
GROUP BY 
    o.ea_guid, 
    o.object_type,
    o.name

HAVING 
    (COUNT(*)>1)

这可能不是你想到的......

答案 1 :(得分:0)

查询有两个错误:

o.ea_guid as CLASSGUID, o.object_type as CLASSTYPE 

意味着结果将在Enterprise Architect模型中引用特定的人工制品,因此您不能在Enterprise Architect中的agregation查询中使用它。使用:

SELECT o.name, COUNT(o.name) as NameCount
FROM t_object as o
GROUP BY o.name
HAVING (COUNT(o.name)>1)

或所有选定的属性应该在GROUP BY子句中:

SELECT o.ea_guid as CLASSGUID, o.object_type as CLASSTYPE, o.name, COUNT(o.name) as NameCount
FROM t_object as o
GROUP BY o.ea_guid , o.object_type , o.name
HAVING (COUNT(o.name)>1)

在这种情况下似乎很愚蠢