select t.deptno,max(t.sumsal) from 
(select deptno,sum(sal) sumsal from emp where hiredate between to_date('1981/1/1','yyyy/mm/dd') and to_date('1981/12/31','yyyy/mm/dd')
group by deptno )t
;
想问为什么啊?

解决方案 »

  1.   

    select deptno,sum(sal) sumsal from emp where hiredate between to_date('1981/1/1','yyyy/mm/dd') and to_date('1981/12/31','yyyy/mm/dd')
    group by deptno 
    内循环是取得同一deptno下的sal的总和,这样得到的结果每个deptno只有一个值吧?你再外层max似乎没有意义
      

  2.   

    max属于分组函数,如果你一定还要分组,你就最后再加一个group by了~~~~
      

  3.   

    是子查询里有问题。子查询里有个sum,所以如果要查出deptno,需要在子查询里group by deptno
      

  4.   

    子查询里面已经group by了,在下一段。。
      

  5.   

    在外层select t.deptno,max(t.sumsal) from 
    就需要再有个group by t.deptno
      

  6.   

    要求sumsal最大的值即该sumsal对应的deptno吧
    改成下面的试试
    select t.deptno,t.sumsal from 
    (select deptno,sum(sal) sumsal,
      dense_rank()over(order by sum(sal) desc)dk from emp where hiredate between to_date('1981/1/1','yyyy/mm/dd') and to_date('1981/12/31','yyyy/mm/dd') 
    group by deptno )t 
    where t.dk=1