加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

为什么不能再where语句中直接使用聚合函数

发布时间:2020-12-14 02:01:01 所属栏目:百科 来源:网络整理
导读:1.问题描述 select deptno,avg(sal) from empwhere count(*)3 group by deptno; 在where 句中使用聚合函数count(*),报出错误:ORA-00934: group function is not allowed here 那是为什么呢? 2.问题解决: 大致解释如下,sql语句的执行过程是:from--wher

1.问题描述

select deptno,avg(sal) from empwhere 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后

聚合函数应该用在having子句中。

聚合函数如果想用在where中的话,应该只能用子查询来实现(在子查询中进行聚合函数计算,然后在主查询中进行where判断)。

代码:

[sql] view plain copy
print ?
  1. selectdeptno,avg(sal)fromempgroupbydeptnohavingcount(deptno)>3;

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读