求助:一个很基础的问题 怎么也想不出来了

vber1010 2009-09-04 12:56:34
如果需要测试脚本请参阅:
http://blog.chinaunix.net/u3/93838/showart_1917196.html
问题是这样的:求出emp表中各个部门工资最多的人的信息,用group by,不要用row_number.

select sum(sal),dept from emp group by dept; 我是想把员工的名字也一起查出来,不知道放在哪。
我写了一个这样的
select ename, sal, deptno
from emp a
where sal = (select max(sal) from emp b where b.deptno = a.deptno);
我感觉用group by 是可以的就是不知道怎么弄了。。。。。。。
...全文
70 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
小灰狼W 2009-09-04
  • 打赏
  • 举报
回复
3楼没看清楚
select ename, sal, deptno
from emp a
where not exists(select 1 from emp where deptno=a.deptno and sal>a.sal)
inthirties 2009-09-04
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 wildwave 的回复:]
还有用分析函数
直接group by 不行,没法对姓名聚合
select ename,sal,deptno from (
select ename, sal, deptno ,
  rank()over(partition by deptno order by sal desc)rk
from emp )
where rk=1

[/Quote]

用这个,如果是并列的用rank(),如果不是并列用 row_number()
vber1010 2009-09-04
  • 打赏
  • 举报
回复
答谢的速度总是跟不上朋友们热情的答题。感觉6楼已经说清楚了。非常感谢楼上朋友们!
vber1010 2009-09-04
  • 打赏
  • 举报
回复
晕 刷新慢了。我是觉得 1 2 楼的别扭。三楼的方法巧妙 但不是我想要的。
小灰狼W 2009-09-04
  • 打赏
  • 举报
回复
还有用分析函数
直接group by 不行,没法对姓名聚合
select ename,sal,deptno from (
select ename, sal, deptno ,
rank()over(partition by deptno order by sal desc)rk
from emp )
where rk=1
vber1010 2009-09-04
  • 打赏
  • 举报
回复
除了楼上两种方法还有其它的吗?总感觉这样写别扭呢。
edicun 2009-09-04
  • 打赏
  • 举报
回复
nice[Quote=引用 2 楼 opps_zhou 的回复:]
SQL codeselect ename, deptno, salfrom scott.empwhere (deptno,sal)in (select deptno,max(sal)as salfrom scott.empgroupby deptno);
[/Quote]
小灰狼W 2009-09-04
  • 打赏
  • 举报
回复
select ename, sal, deptno
from emp a
where not exists(select 1 from emp where sal>a.sal)
opps_zhou 2009-09-04
  • 打赏
  • 举报
回复

select ename, deptno, sal
from scott.emp
where (deptno,sal) in (select deptno, max(sal) as sal from scott.emp group by deptno);
sunwch 2009-09-04
  • 打赏
  • 举报
回复
select ename, sal, deptno
from emp a
where sal = (select max(sal) from emp group by deptno);

17,081

社区成员

发帖
与我相关
我的任务
社区描述
Oracle开发相关技术讨论
社区管理员
  • 开发
  • Lucifer三思而后行
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧