ORA-00979:不是GROUP BY表达式问题

时间:2011-02-17 11:16:58

标签: sql oracle oracle11g ora-00979

我正在执行以下请求,我收到“ORA-00979:不是GROUP BY表达式”错误。

select distinct 
field1, 
field2, 
field3, 
count(*) as field4, 
field5, 
field6,
case
when smt1>0 then 'Deleted'
when smt2>0 then 'Impacted'
when smt3>0 then 'Unknown'
else 'Clean'
end as field7, 
field8, 
field9, 
field10,
field11, 
field12, 
field13
from (<here a big sub query>) A
group by field1, field2
order by field1, field2

我知道我必须将SELECT的所有列放在GROUP BY语句中,除了分组函数(如MAX或SUM),所以我正在尝试以下查询但是我收到相同的错误消息:

select distinct 
field1, 
field2, 
field3, 
count(*) as field4, 
field5, 
field6,
case
when smt1>0 then 'Deleted'
when smt2>0 then 'Impacted'
when smt3>0 then 'Unknown'
else 'Clean'
end as field7, 
field8, 
field9, 
field10,
field11, 
field12, 
field13
from (<here a big sub query>) A
group by field1, field2, field3, field5, field6, field8, field9, field10, field11, field12, field13
order by field1, field2

如何在不改变查询的整体含义的情况下解决这个问题?

非常感谢, 马丁

2 个答案:

答案 0 :(得分:5)

您的field7表达式中缺少group by

此外,您不能通过表达相同查询来在组中使用别名。您需要通过表达式在组中添加完整的CASE语句,以包含field7。

在group by中只提一个别名是不可能的,因为SELECT步骤是执行查询的最后一步,当别名尚未定义时,分组会更早发生。

答案 1 :(得分:3)

您需要添加表达式

case
when smt1>0 then 'Deleted'
when smt2>0 then 'Impacted'
when smt3>0 then 'Unknown'
else 'Clean'
end

进入group by表达式。