如何通过一句简单的SQL 求一个常见的平均数问题:
在一个数据集合里,去掉最大,最小值的一个数 然后取剩下的那堆数字的平均数.就好像跳水,体操比赛的平均得分那样子.好像最高10分的,可能有N个,但只去掉一个而已

解决方案 »

  1.   

    select (sum(a)-max(a)-min(a))/(count(a)-2) from ...
      

  2.   

    写个手工的吧!
    --先把最大和最小的剔除,然后求平均
    --注意不能求max,min,防止重复
    create table test(
    col   num);select avg(num) from (
    select num,rownum rn2 from(
    select num,rownum rn from test t order by num)
    where rn<>1
    order by num desc)
    where rn<>1
      

  3.   

    select (sum(a)-max(a)-min(a))/(count(a)-2) from ...
    这个是最好的办法了
      

  4.   

    这个也行,刚试过
    select avg(col) from tem where not exists 
    (select max(col) from tem
            union select min(col) from tem)
      

  5.   

    刚刚搞错了,正确的应该是这样的!select avg(col) from tem where col not in 
    (select max(col) from tem
            union select min(col) from tem)
      

  6.   

    select (sum(a)-max(a)-min(a))/(count(a)-2) from ... 
    这个是最好的办法了
    支持一下
      

  7.   

    用select (sum(a)-max(a)-min(a))/(count(a)-2) from ...  这个办法
    如果这个数据集合里 刚好只有两个数,除数就变为0了.所以上面的sql必须保证count(a)>2
      

  8.   

    Select (sum(colc)-max(colc)-min(colc))/decode( count(colc)-2 ,0,1,count(colc)-2)
    除的时候判断下就好了,这方法相当的强