oracle group by的一道问题

来源:百度知道 编辑:UC知道 时间:2024/06/11 15:42:12
要求显示:合计部门工资(sal)最高的部门号码(deptno)及确切的合计工资数。

新手,我只知道 select deptno, sum(sal) from emp group by deptno;

如果我用select deptno, max(sum(sal))from emp group by deptno; 就会提示我嵌套太深,无法查询,或者是单一组无法group by 请会的指教一下,有多少分我全部压上,谢!
我的意思是 可能有10个或n个部门 每个部门都有一个合计工资 然后要找出合计工资最高的那个部门

select * from
(
select deptno, sum(sal) sumsal from emp group by deptno order by sumsal desc
)
where rownum <2

LZ请确定如果结果可能出现并列第一的现象,请使用:
select a.deptno, a.sumsal
from (select deptno, sum(sal) sumsal, rank() over(order by sum(sal) desc) rk
from emp
group by deptno
) a
where rk < 2

select deptno, sum(sal)
from emp
group by deptno
order by sum(sal) DESC
where rownum=1

select depton,sum(sal) over(partition by deptno order by sal desc) mysal
form emp
order by 1,2 desc

这里使用开窗函数,速度最快。

select distinct deptno, max(sum(sal))from emp group by deptno

你是要这个效果吧 你那deptno是主键的话 sum(sal)就应该是一条记录 不用max了 如果不放心的话 加上 and rownum=1 吧

不知是否回答了你的问题

SQL好像不用忘了。。