2014-03-05 30 views
0

我有以下查詢,返回的錯誤信息"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) 

現在,如果我加入了GROUP BY子句中o.ea_guid & o.object_type,沒有結果返回

我在做什麼錯?

回答

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) 

這可能不是你的想法...

+0

這也不起作用愚蠢。我收到消息''你試圖執行一個查詢,該查詢不包含指定的表達式'Name'作爲聚合函數「'的一部分。如果我將它包含在GROUP BY子句中,則不會返回任何結果 – PeanutsMonkey

+0

對不起,存在拼寫錯誤 - 我做了一個快速更改,但忘記將GROUP BY子句添加「o.name」。我現在將它添加到上面。你明白爲什麼會導致錯誤? –

0

查詢有兩個錯誤:

o.ea_guid as CLASSGUID, o.object_type as CLASSTYPE 

意味着結果將被引用到Enterprise Architect的模型特別是假象,因此您不能在Enterprise Architect中的聚合查詢中使用它。用途:

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) 

這似乎在這種情況下

相關問題