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

    xiaoxiao2025-10-06  1

    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://ju.6miu.com/read-1302894.html
    最新回复(0)