日期:2014-05-17  浏览次数:20771 次

group by
查询emp、dept表中哪些部门的平均工资比30部门的平均工资高,并按部门编号降序排列,要求列出部门编号,部门名,平均工资。我的做法是这样的:
select e.deptno,d.dname,avg(e.sal)
from emp e join dept d
on e.deptno=d.deptno
group by e.deptno 
having avg(e.sal)>(
select avg(sal) 
from emp 
where deptno=30
)order by deptno desc;
出现错误:
select e.deptno,d.dname,avg(e.sal
               *
第 1 行出现错误:
ORA-00979: 不是 GROUP BY 表达式

这条查询语句在mysql中是可以的,但是在oracle中不行,求高手指点。

------解决方案--------------------

select e.deptno,d.dname,avg(e.sal)
from emp e join dept d
on e.deptno=d.deptno
group by e.deptno 
,d.dname --加上这句
having avg(e.sal)>(
select avg(sal) 
from emp 
where deptno=30
)order by deptno desc;

------解决方案--------------------
d.dname没写在group by 表达式内。