现有表   A:
字段1为字符型,字段2为字符型,字段3为数值型,字段4为数值型
——————————————————————
表A内容如下。
-------------------------------------------
字段1             字段2                       字段3                  字段4
2001                 wm                           12.5                  1
2001                 wm                           12.5                  1
2001                 wm                           0                     1
2001                 wm                           14                    1 
2002                 wm                           11                    1
2002                 wm                           12                    1
2002                 wm                           13                    1现在要求以下结果:
-------------------------------------------
字段1             字段3的平均值(不把等于0的算在内)        字段4的和
2001                 13.5                                     4
2002                 12                                       3
-------------------------------------------
请高手们指教

解决方案 »

  1.   


    select a.字段1,字段3的平均值,sum(字段4)字段4的和
    from
    (select 字段1 ,avg(字段3) 字段3的平均值 from A where 字段3>0 group by 字段1)b,a
    where a.字段1=b.字段1
     group by a.字段1
      

  2.   

    select 字段1,sum(字段3)/sum(case 字段3 when 0 then 0 else 1 end) as '字段3的平均值',sum(字段4) as '字段4的和' from A group by 字段1
      

  3.   

    select 字段1,avg(case when 字段3 <> 0 then 字段3 else null end),sum(字段4) from A group by 字段1
      

  4.   

    select 字段1 , avg(字段3) 字段3的平均值 , (select sum( 字段4) from A where 字段1 = t.字段1) 字段4的和 from tb t where 字段3 <> 0
      

  5.   

    select 字段1 ,sum(字段3)/sum(case when 字段3 = 0 then 0 else 1 end ) as [字段3的平均值(不把等于0的算在内)] ,  sum( 字段4 ) [字段4的和]
    from a 
    group by 字段1
      

  6.   

    select 字段1 , avg(字段3) 平均值 , (select sum( 字段4) from A where 字段1 = t.字段1) 字段4的和 from tb t 
    group by 字段1
      

  7.   

    select 字段1,avg
    (case when
    字段3<>0 then
    字段3
    else
    null
    end) AS 字段3的平均值,
    sum(字段4) AS 字段4的和 from A
    group by 字段1
      

  8.   

    因为某一行数据在求字段4的和时需要但可能在求字段3的平均值时不需要,所以不能直接Group By字段1,应该是先分别查询得到字段3的平均值和字段4的和再连接二个查询即可~实现如下:
    Select average.字段1,average.字段3的平均值,summation.字段4的和 From 
     (--求字段3平均值
      Select 字段1,Avg(字段3) AS 字段3的平均值 From A Where 字段3>0 Group By 字段1
     ) AS average
     Inner Join
     (--求字段4的和
       Select 字段1,Sum(字段4) AS 字段4的和 From A Group By 字段1
     ) AS summation On average.字段1=summation.字段1
    Order by average.字段1--如果我对你的说明理解正确的话,应该是这样的
      

  9.   

    select 字段1,sum(字段3)/sum(case 字段3  when  0  then  0  else  1  end)  as  '字段3的平均值',sum(字段4)   as   '字段4的和'   from   A   
    group   by   字段1
      

  10.   

    LZ
    3楼是正解,因为SQL SERVER 的avg不会将NULL纳入计算,自然就将=0的给排除了。
      

  11.   

    楼主这样也行select 字段1,sum(字段3)/sum(case when 字段3 = 0 then 0 else 1 end),sum(字段4)
    from t
    group by 字段1
      

  12.   

    declare @aa table([字段1] nvarchar(10),[字段2] nvarchar(10),[字段3] decimal(13,2) ,  [字段4] int )
    insert into @aa select '2001', 'wm',  12.5 ,1 
    insert into @aa select '2001', 'wm',  12.5 ,1 
    insert into @aa select '2001', 'wm',  0 ,1 
    insert into @aa select '2001', 'wm',  14 ,1 
    insert into @aa select '2002', 'wm',  11 ,1 
    insert into @aa select '2002', 'wm',  12 ,1 
    insert into @aa select '2002', 'wm',  13,1 select [字段1] ,sum([字段3])/sum(case 字段3  when  0  then  0  else  1  end)  ,sum([字段4]) from @aa group by [字段1]
    --结果
    (1 row(s) affected)(1 row(s) affected)(1 row(s) affected)(1 row(s) affected)(1 row(s) affected)(1 row(s) affected)(1 row(s) affected)
    字段1                                                
    ---------- --------------------------------------- -----------
    2001       13.000000                               4
    2002       12.000000                               3(2 row(s) affected)