码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2019-02-10 23:44:57      阅读:366      评论:0      收藏:0      [点我收藏+]

标签:结果   题解   出错   sql   运行   HERE   avg   color   大致   

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

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

标签:结果   题解   出错   sql   运行   HERE   avg   color   大致   

原文地址:https://www.cnblogs.com/brianlai/p/10360437.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!