用简洁的sql 查询出平均值的最大值

zktthxq 2012-11-28 03:26:08
表workload(empid,workdate,hours),用一条sql查询出平均每天工作小时数最多的员工id,试了很多,都感觉写的很累赘,

select empid
from (select empid, avg(hours) as avghours
from workload
group by empid) c
where c.avghours>= (select max(avghours)
from (select empid, avg(hours) as avghours
from workload
group by empid))
用了两次相同的子查询,
谁能用简洁的一条sql搞定呢,麻烦献上一条哦,感谢!!!
...全文
1639 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
raymonshi 2012-12-13
  • 打赏
  • 举报
回复
引用 7 楼 zjzhangkui 的回复:
哦,rank 就是把row_number() 函数换成 rank(), 学了一招了,非常感谢了
前段比较忙,没有上来看。恭喜
zktthxq 2012-11-28
  • 打赏
  • 举报
回复
哦,rank 就是把row_number() 函数换成 rank(), 学了一招了,非常感谢了
zktthxq 2012-11-28
  • 打赏
  • 举报
回复
感觉有点高级了,试卷里面会考rank这个用法吗? 不知道出题者到底需要怎么样的答案
zktthxq 2012-11-28
  • 打赏
  • 举报
回复
引用 2 楼 hebeishimeng 的回复:
引用 1 楼 BenChiM888 的回复: SQL code?123456789select empid from( select empid, avg(hours), row_number()over(order by avg(hours) desc) as rn from workload group by empid)where rn = 1 -- rn =……
需要查询出并列的,用rank 怎么写呢?
小海葵1 2012-11-28
  • 打赏
  • 举报
回复
rank和dense_rank我怎么就没想到呢。
小海葵1 2012-11-28
  • 打赏
  • 举报
回复 1
SELECT empid FROM (SELECT AVG(hours), empid FROM workload GROUP BY empid ORDER BY AVG(hours) DESC) WHERE ROWNUM = 1
raymonshi 2012-11-28
  • 打赏
  • 举报
回复
引用 1 楼 BenChiM888 的回复:
SQL code?123456789select empid from( select empid, avg(hours), row_number()over(order by avg(hours) desc) as rn from workload group by empid)where rn = 1 -- rn = 1 平均时长第一,r……
正解, 分析函数,改成rank更好吧,可以查询出并列的。
BenChiM888 2012-11-28
  • 打赏
  • 举报
回复

select empid
  from( 
select empid, 
       avg(hours),
       row_number()over(order by avg(hours) desc) as rn
  from workload
 group by empid
)where rn = 1 -- rn = 1 平均时长第一,rn=2 平均时长第二 通用不?

17,078

社区成员

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

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