表T
字段A,B,C
有三条记录A  B  C 
0 20 30
10 0  0
60 90 0求A,B,C的平均值,0的记录不做统计,如A列,结果为(10+60)/2要求一条语句实现

解决方案 »

  1.   

    SELECT avg(A),avg(B),avg(C) from table where A>0 AND B>0 AND C>0
      

  2.   

    select sum(a)/sum(case a when 0 then 0 else 1 end) as a,
           sum(b)/sum(case b when 0 then 0 else 1 end) as b,
           sum(c)/sum(case c when 0 then 0 else 1 end) as c,
    from t
      

  3.   

    SELECT sum(A)/sum(case when A=0 then 0 else 1 end)
        ,sum(B)/sum(case when B=0 then 0 else 1 end)
        ,sum(C)/sum(case when C=0 then 0 else 1 end)
     from table
      

  4.   

    select
        (a.A+a.B+a.C)/(case a.D when 0 then 1 else a.D end)
    from
        (select
             A,B,C,
             (case A when 0 then 0 else 1 end)+(case B when 0 then 0 else 1 end)+(case C when 0 then 0 else 1 end) as D
         from
             T) a
      

  5.   

    有个缺陷,sum(case when A=0 then 0 else 1 end)为0的话报错
      

  6.   

    select 
        sum(A)/(case sum(case A when 0 then 0 else 1 end) when 0 then 1 else sum(case A when 0 then 0 else 1 end) end),
        sum(B)/(case sum(case B when 0 then 0 else 1 end) when 0 then 1 else sum(case B when 0 then 0 else 1 end) end),
        sum(C)/(case sum(case C when 0 then 0 else 1 end) when 0 then 1 else sum(case C when 0 then 0 else 1 end) end)
    from
        T
      

  7.   

    SELECT DISTINCT 
    A=(SELECT AVG(A) FROM 表 WHERE A>0),
    B=(SELECT AVG(B) FROM 表 WHERE B>0),
    C=(SELECT AVG(C) FROM 表 WHERE C>0) 
    FROM 表
      

  8.   

    select sum(a)/(select count(*) from abc where a<>0) as avg_a,
    sum(b)/(select count(*) from abc where b<>0) as avg_b,
    sum(c)/(select count(*) from abc where c<>0) as avg_c
     from abc
      

  9.   

    declare @t table(A int,B int,C int)
    insert into @t select  0,20,30
    insert into @t select 10, 0, 0
    insert into @t select 60,90, 0
    select 
        sum(A)/(case sum(case A when 0 then 0 else 1 end) when 0 then 1 else sum(case A when 0 then 0 else 1 end) end) as A,
        sum(B)/(case sum(case B when 0 then 0 else 1 end) when 0 then 1 else sum(case B when 0 then 0 else 1 end) end) as B,
        sum(C)/(case sum(case C when 0 then 0 else 1 end) when 0 then 1 else sum(case C when 0 then 0 else 1 end) end) as C
    from
        @t/*
    A           B           C           
    ----------- ----------- ----------- 
    35          55          30
    */
      

  10.   

    select (sum(A)/(select count(A) from tablename where A>0))AS A,(sum(B)/(select count(B) from tablename where B>0))AS B,(sum(C)/(select count(C) from tablename where C>0))AS C from tablename
      

  11.   

    select case sum(a) when 0 then 0 else sum(a)/sum(case a when 0 then 0 else 1 end) end as a,
           case sum(b) when 0 then 0 else  sum(b)/sum(case b when 0 then 0 else 1 end) end as b,
           case sum(c) when 0 then 0 else  sum(c)/sum(case c when 0 then 0 else 1 end) end as c,
    from t
      

  12.   

    zlp321002(众里寻它千百度,蓦然回首,那人却在灯火阑珊处。) 厉害阿厉害!
      

  13.   

    借鉴zlp321002的:
    -------------------------------------------------------------------
    declare @t table(A int,B int,C int)
    insert into @t select  0,20,30
    insert into @t select 10, 0, 0
    insert into @t select 60,90, 0
    SELECT
        A=isnull((SELECT AVG(A) FROM @t WHERE A<>0),0),
        B=isnull((SELECT AVG(B) FROM @t WHERE B<>0),0),
        C=isnull((SELECT AVG(C) FROM @t WHERE C<>0),0) /*
    A           B           C           
    ----------- ----------- ----------- 
    35          55          30
    */
      

  14.   

    declare @t table(A int,B int,C int)
    insert into @t select  0,20,30
    insert into @t select 10, 0, 0
    insert into @t select 60,90, 0
    select (sum(A)/(select count(A) from @t where A>0))AS A,(sum(B)/(select count(B) from @t where B>0))AS B,(sum(C)/(select count(C) from @t where C>0))AS C from @t
    结果:
    -------------------------
    A           B           C           
    ----------- ----------- ----------- 
    35          55          30(所影响的行数为 1 行)
      

  15.   

    谢谢诸位,我也是使用libin_ftsafe(子陌红尘:当libin告别ftsafe)的方法,
    zlp321002(众里寻它千百度,蓦然回首,那人却在灯火阑珊处。) 和qw12cn() 的由于表T的条件复杂(是我简化了)不做考虑,结帐了
      

  16.   

    SELECT  DISTINCT
    A=(SELECT AVG(A) FROM temp WHERE A<>0),
    B=(SELECT AVG(B) FROM temp WHERE B<>0),
    C=(SELECT AVG(C) FROM temp WHERE C<>0) 
    FROM temp
      

  17.   

    create table T
    (
    a int,
    b int,
    c int
    )
    go
    insert into T (a,b,c) values(0,20,30)
    go
    insert into T(a,b,c) values(10,0,0)
    go
    insert into T(a,b,c) values(60,90,0)
    go
    select sum(a)/sum(case a when 0 then 0 else 1 end) as a,
           sum(b)/sum(case b when 0 then 0 else 1 end) as b,
           sum(c)/sum(case c when 0 then 0 else 1 end) as c from T
    go
    大家可以试一下这种算法,这个问题不难!!!
      

  18.   

    create   table test (A int,B int, C int)
    insert into test
    select 0,20,30
    union all 
    select 10,0,0
    union all
    select 60,90,0select sum(A)/sum(case when A=0 then 0 else 1 end) as AverageA,
    sum(B)/sum(case when B=0 then 0 else 1 end) as AverageB,
    sum(C)/sum(case when C=0 then 0 else 1 end)as AverageC
     from test