ORA-00979:执行sql时不是GROUP BY表达式

ORA-00979:执行sql时不是GROUP BY表达式,sql,oracle,Sql,Oracle,当我执行SQL时,我得到以下错误: 我的例外是: [SQL]select bd.name aaa from bd_material_v m ,bd_marbasclass bd where m.creator='1001A210000000000IIQ' and bd.code=substr(m.code,0,4) group by bd.name order by substr(m.code,0,4) [Err] ORA-00979: not a GROUP

当我执行SQL时,我得到以下错误:

我的例外是:

[SQL]select bd.name aaa   
from bd_material_v m ,bd_marbasclass bd  
where m.creator='1001A210000000000IIQ'  
and bd.code=substr(m.code,0,4)    
group by bd.name    
order by substr(m.code,0,4)  
[Err] ORA-00979: not a GROUP BY expression
我的SQL如下:

select bd.name aaa 
  from bd_material_v m ,bd_marbasclass bd
    where m.creator='1001A210000000000IIQ'
      and bd.code=substr(m.code,0,4)  
      group by bd.name 
      order by substr(m.code,0,4)

附加的

如果我按bd.name注释这一行
分组
,我将查询出结果,而不会出现问题

编辑

我的桌子:

bd_marbasclass

bd_材料_v



问题在于,您是按
group by
子句中不包含的内容进行订购的

例如,这是有效的

SQL> with testGroup as ( select 1 as one, 2 as two from dual)
  2  select one
  3  from testGroup
  4  group by one;

       ONE
----------
         1
如果您的
orderby
列不在
groupby
子句中:

SQL> with testGroup as ( select 1 as one, 2 as two from dual)
  2  select one
  3  from testGroup
  4  group by two;
select one
       *
ERROR at line 2:
ORA-00979: not a GROUP BY expression
如果编辑
group by
子句以处理
order by
中所需的列:

SQL> with testGroup as ( select 1 as one, 2 as two from dual)
  2  select one
  3  from testGroup
  4  group by one, two;

       ONE
----------
         1

SQL>

问题是,您是按
group by
子句中不包含的内容进行订购的

例如,这是有效的

SQL> with testGroup as ( select 1 as one, 2 as two from dual)
  2  select one
  3  from testGroup
  4  group by one;

       ONE
----------
         1
如果您的
orderby
列不在
groupby
子句中:

SQL> with testGroup as ( select 1 as one, 2 as two from dual)
  2  select one
  3  from testGroup
  4  group by two;
select one
       *
ERROR at line 2:
ORA-00979: not a GROUP BY expression
如果编辑
group by
子句以处理
order by
中所需的列:

SQL> with testGroup as ( select 1 as one, 2 as two from dual)
  2  select one
  3  from testGroup
  4  group by one, two;

       ONE
----------
         1

SQL>

请改用“选择不同的”:

select distinct bd.name aaa , 
       substr(m.code,0,4) -- see below code comment, either include this or remove the order by
from bd_material_v m
inner join bd_marbasclass bd
      on bd.code=substr(m.code,0,4)  
where m.creator='1001A210000000000IIQ'
order by substr(m.code,0,4) -- As this is oracle, you can only order by selected columns/expressions :D

请改用“选择不同的”:

select distinct bd.name aaa , 
       substr(m.code,0,4) -- see below code comment, either include this or remove the order by
from bd_material_v m
inner join bd_marbasclass bd
      on bd.code=substr(m.code,0,4)  
where m.creator='1001A210000000000IIQ'
order by substr(m.code,0,4) -- As this is oracle, you can only order by selected columns/expressions :D

尽管您的连接语法是非常规的,但代码中的问题是
orderby
。如果将其删除,您将不会得到
非分组依据
错误

但我强烈建议更正连接语法。在适当的列上使用
内部联接

select bd.name aaa 
  from bd_material_v m ,bd_marbasclass bd
    where m.creator='1001A210000000000IIQ'
      and bd.code=substr(m.code,0,4)  
      group by bd.name 
所以这不会给你错误

with tbl(col1,col2) as 
(select 1,1 from dual union all
select 1,3 from dual )
select col1 as col1 
from tbl 
group by col1
但这会

with tbl(col1,col2) as 
(select 1,1 from dual union all
select 1,3 from dual )
select col1 as col1 
from tbl 
group by col1
order by col2

尽管您的连接语法是非常规的,但代码中的问题是
orderby
。如果将其删除,您将不会得到
非分组依据
错误

但我强烈建议更正连接语法。在适当的列上使用
内部联接

select bd.name aaa 
  from bd_material_v m ,bd_marbasclass bd
    where m.creator='1001A210000000000IIQ'
      and bd.code=substr(m.code,0,4)  
      group by bd.name 
所以这不会给你错误

with tbl(col1,col2) as 
(select 1,1 from dual union all
select 1,3 from dual )
select col1 as col1 
from tbl 
group by col1
但这会

with tbl(col1,col2) as 
(select 1,1 from dual union all
select 1,3 from dual )
select col1 as col1 
from tbl 
group by col1
order by col2

错误非常简单-
aaa
不是一个分组表达式。你能分享你的表结构、一些示例数据和你想要达到的结果吗?@Mureinik当然,我会的,顺便说一句,请学习如何使用
JOIN
。这种旧式的连接在10多年前就被淘汰了。你可以找到很多很多答案。这个错误非常简单-
aaa
不是一组表达式。你能分享你的表结构、一些示例数据和你想要达到的结果吗?@Mureinik当然,我会的,顺便说一句,请学习如何使用
JOIN
。10多年前,这种旧式的加入方式被逐步淘汰。你可以找到很多答案,谢谢,但我有一个错误:
不是一个选定的表达式
这是我会做的,也不需要这里的
分组。我不知道为什么这个答案没有被投票。谢谢,但我得到了一个错误:
不是一个选定的表达式
这是我会做的,而且这里不需要
分组。我不知道为什么这个答案没有被选上。它不是非传统的,它是过时的,古人的,它不是非传统的,它是过时的,古老的