首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > oracle >

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

2012-12-29 
用简洁的sql 查询出平均值的最大值表workload(empid,workdate,hours),用一条sql查询出平均每天工作小时数

用简洁的sql 查询出平均值的最大值
表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搞定呢,麻烦献上一条哦,感谢!!!
[解决办法]

引用:
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更好吧,可以查询出并列的。
[解决办法]
SELECT empid
  FROM (SELECT AVG(hours), empid
          FROM workload
         GROUP BY empid
         ORDER BY AVG(hours) DESC)
 WHERE ROWNUM = 1

热点排行