Select avg(字段) from 表 where 字段>0
Select avg(字段) from 表 where 字段<0

解决方案 »

  1.   

    Select avg(字段) from 表 where 字段>=0
    Select avg(字段) from 表 where 字段<0
      

  2.   

    --分开处理就行了.
    Select avg(字段) from 表 where 字段>0
    Select avg(字段) from 表 where 字段<0
      

  3.   

    如果不是数值型的可以转化为数值型的cast()  
    select avg(数值型字段) '平均值' from 表名 where 数值型字段>0
    select avg(数值型字段) '平均值' from 表名 where 数值型字段<0
      

  4.   

    select top 1
    (select avg(字段) from 表 where a>0) 大于0平均值,
    (select avg(字段) from 表 where a<0) 小于0平均值
    from 表
      

  5.   

    --动态SQL语句数据处理
    declare @s11 varchar(8000),@s12 varchar(8000)
    declare @s21 varchar(8000),@s22 varchar(8000)
    select @s11='',@s12='',@s21='',@s22=''
    select @s11=@s11+'+case when '+name+'>0 then '+name+' else 0 end'
    ,@s12=@s12+'+case when '+name+'>0 then 1.0 else 0 end'
    ,@s21=@s21+'-case when '+name+'<0 then '+name+' else 0 end'
    ,@s22=@s22+'+case when '+name+'<0 then 1.0 else 0 end'
    from syscolumns where object_id('你的表')=id          --修改此处的表名
    exec('select 正数平均=case a2 when 0 then 0 else a1/a2 end
    ,负数平均=case b2 when 0 then 0 else b1/b2 end
    from(
    select a1=('+@s11+'),a2=('+@s12+'),b1=('+@s21+'),b2=('+@s22+') from 你的表) a')  --修改此处的表名为你的表名
      

  6.   

    --数据测试表
    create table tb(a int,b int,c int)
    insert into tb
    select 1,1,1
    union all select 1,2,3
    union all select -1,12,3
    union all select 1,2,-3
    union all select 1,-292,3
    union all select -1,2,3--数据处理
    declare @s11 varchar(8000),@s12 varchar(8000)
    declare @s21 varchar(8000),@s22 varchar(8000)
    select @s11='',@s12='',@s21='',@s22=''
    select @s11=@s11+'+case when '+name+'>0 then '+name+' else 0 end'
    ,@s12=@s12+'+case when '+name+'>0 then 1.0 else 0 end'
    ,@s21=@s21+'-case when '+name+'<0 then '+name+' else 0 end'
    ,@s22=@s22+'+case when '+name+'<0 then 1.0 else 0 end'
    from syscolumns where object_id('tb')=id
    exec('select 正数平均=case a2 when 0 then 0 else a1/a2 end
    ,负数平均=case b2 when 0 then 0 else b1/b2 end
    from(
    select a1=('+@s11+'),a2=('+@s12+'),b1=('+@s21+'),b2=('+@s22+') from tb) a')
    go--删除测试表
    drop table tb/*--测试结果
    正数平均                负数平均                
    ------------------- ------------------- 
    1.000000            .000000
    2.000000            .000000
    7.500000            1.000000
    1.500000            3.000000
    2.000000            292.000000
    2.500000            1.000000
    --*/
      

  7.   

    邹大侠,能不能再问一下,你的from(
    select a1=('+@s11+'),a2=('+@s12+'),b1=('+@s21+'),b2=('+@s22+') from tb) a')中的最后一个a是什么意思呀
      

  8.   

    这个问题我明白了,你真的很高呀.还有这个处理过程中,循环处理的部分在什么地方是不是在
    select @s11=@s11+'+case when '+name+'>0 then '+name+' else 0 end'
    ,@s12=@s12+'+case when '+name+'>0 then 1.0 else 0 end'
    ,@s21=@s21+'-case when '+name+'<0 then '+name+' else 0 end'
    ,@s22=@s22+'+case when '+name+'<0 then 1.0 else 0 end'
    from syscolumns where object_id('tb')=id
    中把所有的符合条件的数据都处理了,还是像指针一样依次下移然后再重复执行
    select @s11=@s11+'+case when '+name+'>0 then '+name+' else 0 end'
    ,@s12=@s12+'+case when '+name+'>0 then 1.0 else 0 end'
    ,@s21=@s21+'-case when '+name+'<0 then '+name+' else 0 end'
    ,@s22=@s22+'+case when '+name+'<0 then 1.0 else 0 end'
    from syscolumns where object_id('tb')=id
    exec('select 正数平均=case a2 when 0 then 0 else a1/a2 end
    ,负数平均=case b2 when 0 then 0 else b1/b2 end
    from(
    select a1=('+@s11+'),a2=('+@s12+'),b1=('+@s21+'),b2=('+@s22+') from tb) a')
    go
    这些,尽管有点罗嗦,可能不屑回答,可我真的想充分了解这个处理过程,请在麻烦一次,谢谢了
      

  9.   

    对.select @s11=@s11+'+case when '+name+'>0 then '+name+' else 0 end'
    ,@s12=@s12+'+case when '+name+'>0 then 1.0 else 0 end'
    ,@s21=@s21+'-case when '+name+'<0 then '+name+' else 0 end'
    ,@s22=@s22+'+case when '+name+'<0 then 1.0 else 0 end'
    from syscolumns where object_id('tb')=id上面循环处理所有字段名.动态生成要处理的SQL语句.下面才是真正执行的语句.exec('select 正数平均=case a2 when 0 then 0 else a1/a2 end
    ,负数平均=case b2 when 0 then 0 else b1/b2 end
    from(
    select a1=('+@s11+'),a2=('+@s12+'),b1=('+@s21+'),b2=('+@s22+') from tb) a')
    go