求平均值

ttyp 2006-12-15 02:16:31
表T
字段A,B,C
有三条记录

A B C
0 20 30
10 0 0
60 90 0

求A,B,C的平均值,0的记录不做统计,如A列,结果为(10+60)/2

要求一条语句实现
...全文
1162 27 打赏 收藏 转发到动态 举报
写回复
用AI写文章
27 条回复
切换为时间正序
请发表友善的回复…
发表回复
xihii6 2006-12-20
  • 打赏
  • 举报
回复
ding
zibetqzy 2006-12-19
  • 打赏
  • 举报
回复
dd
cnrefresh 2006-12-18
  • 打赏
  • 举报
回复
store
caijiek 2006-12-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,0

select 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
秋水森 2006-12-18
  • 打赏
  • 举报
回复
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
大家可以试一下这种算法,这个问题不难!!!


qw12cn 2006-12-15
  • 打赏
  • 举报
回复
子陌红尘的好
BEN_717_GG 2006-12-15
  • 打赏
  • 举报
回复


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
ttyp 2006-12-15
  • 打赏
  • 举报
回复
谢谢诸位,我也是使用libin_ftsafe(子陌红尘:当libin告别ftsafe)的方法,
zlp321002(众里寻它千百度,蓦然回首,那人却在灯火阑珊处。) 和qw12cn() 的由于表T的条件复杂(是我简化了)不做考虑,结帐了
caixia615 2006-12-15
  • 打赏
  • 举报
回复
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 行)
子陌红尘 2006-12-15
  • 打赏
  • 举报
回复
借鉴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
*/
冷箫轻笛 2006-12-15
  • 打赏
  • 举报
回复
zlp321002(众里寻它千百度,蓦然回首,那人却在灯火阑珊处。)

厉害阿厉害!
冷箫轻笛 2006-12-15
  • 打赏
  • 举报
回复
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
caixia615 2006-12-15
  • 打赏
  • 举报
回复
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
子陌红尘 2006-12-15
  • 打赏
  • 举报
回复
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
*/
qw12cn 2006-12-15
  • 打赏
  • 举报
回复
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
zlp321002 2006-12-15
  • 打赏
  • 举报
回复
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 表
子陌红尘 2006-12-15
  • 打赏
  • 举报
回复
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
青锋-SS 2006-12-15
  • 打赏
  • 举报
回复
问题不小,全过滤掉了.
ttyp 2006-12-15
  • 打赏
  • 举报
回复
有个缺陷,sum(case when A=0 then 0 else 1 end)为0的话报错
冷箫轻笛 2006-12-15
  • 打赏
  • 举报
回复
菜大侠题意理解的有点问题哈
加载更多回复(6)

34,576

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧