1.问题描述

select deptno ,avg(sal) from emp where count(*)>3 group by deptno; 在where 句中使用聚合函数count(*),报出错误:ORA-00934: group function is not allowed here

那是为什么呢?

2.问题解决:

大致解释如下,sql语句的执行过程是:from-->where-->group by -->having --- >order by --> select;

聚合函数是针对结果集进行的,但是where条件并不是在查询出结果集之后运行,所以主函数放在where语句中,会出现错误,

而having不一样,having是针对结果集做筛选的,所以我门一般吧组函数放在having中,用having来代替where,having一般跟在group by后

 

 正确代码:

 select deptno,avg(sal) from emp group by deptno having count(deptno)>3;

 

 

 

来自:https://blog.csdn.net/w2232097312/article/details/52211201

内容来源于网络如有侵权请私信删除
你还没有登录,请先登录注册
  • 还没有人评论,欢迎说说您的想法!