6.求出带‘S’字符的雇员所在的部门的工资合计和其部门的人数
上面这个可以正常查询,但
select sum((select sal from emp p where e.deptno=p.deptno)),count((select sal from emp p where e.deptno=p.deptno)) from emp e where ename like '%S%' group by deptno ;
错误!为什么不能呢?
第 1 行出现错误:
ORA-01427: 单行子查询返回多个行
select (select sum(sal) from emp p where e.deptno=p.deptno),(select count(sal) from emp p where e.deptno=p.deptno) from emp e where ename like '%S%' group by deptno ;
因为统计函数里面只能是单个列名,通过这个列名统计函数可以统计到多行数据,而select是一个列里面包含的多行数据所以两者是有差别的,因此可以如上将统计函数放在里面。
select job,count(*),avg(sal) from emp group by job having min(avg(sal));
第 1 行出现错误:
ORA-00935: 分组函数的嵌套太深
select job,count(*),avg(sal) from emp group by job having avg(sal) = (select min(avg(sal)) from emp group by job) ;
Having 不能嵌套统计函数