点击此处浏览总目录

计算平均值时去掉最大值和最小值

描述:

  当计算平均值时,通过去掉最大值和最小值,以降低它们对最终计算结果的影响
  例如,统计员工的平均工资时,先去掉最高工资和最低工资,再计算全体员工的平均工资

 

解决方案:

with dataset as (
    select 'Jack' as Name,100 as Sales from dual
    union all
    select 'Tom' as Name,120 as Sales from dual
    union all
    select 'Jimmy' as Name,10 as Sales from dual
    union all
    select 'Frank' as Name,999 as Sales from dual
    union all
    select 'John' as Name,105 as Sales from dual
)

select avg(Sales)
from 
(
    select Name,
            Sales,
            min(Sales)over() as min_sales,
            max(Sales)over() as max_sales
    from dataset
) t
where t.Sales not in (t.min_sales,t.max_sales)

结果:

  AVG(SALES)

  108.333333333333

posted @ 2020-02-25 11:27  立业的博客  阅读(2420)  评论(0编辑  收藏  举报