当前位置: 移动技术网 > IT编程>数据库>Oracle > where语句中不能直接使用聚合函数

where语句中不能直接使用聚合函数

2019年02月11日  | 移动技术网IT编程  | 我要评论

黑基,人才市场报,二姐性事

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

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网