我想做求1组数的平均值,但是如果其中一个数是零就不参加计算。
需要怎么写呢?似乎很简单。就是不会,呵呵!
各位谁会帮帮忙。谢谢!30分笑纳~

解决方案 »

  1.   

    select case when t.num <> 0 then avg(t.num) end from table t
      

  2.   

    用函数nvl
    欢迎加入oracle qq群9701750
      

  3.   

    先举个例子吧:   ID    A1  
                    1    20
                    2    30
                    3     0
                    4    40
                    5    110我想求A1的平均值,如果AVG(A1)的话就是(20+30+0+40+110)/5=40,
    而我想算不包含0的数据,也就是(20+30+40+110)/4=50。
    请问要怎么才能解决这个问题。有没函数呢?我不想再判断,因为数据项很多!太麻烦了。
      

  4.   

    直接用条件把等于0的过滤掉然后用AVG函数不就可以了
    select avg(col) from table where col<> 0
      

  5.   

    你這樣是不行的啊!
    select sum/count from 
       (select sum(a1) sum from a) a,(select count(a1) count from a where nvl(a1,0)<>0) b
      

  6.   


    SORRY
    這種方式也是可以的啊!
      

  7.   

    用decode函数处理下再avg。
    with t as (
      select 20 id from dual
      union all
      select 30 from dual
      union all
      select 0 from dual
      union all
      select 40 from dual
      union all
      select 110 from dual
      union all
      select 0 from dual
    )
    select avg(decode(id,0,null,id)) from t;
      

  8.   

    select avg(case when id = 0 then null else id end) from t;