SQL 求平均值问题 !

tianzhenbo 2008-03-21 09:31:35
求一行的平均值格式如下:
字段1 字段2 字段3 字段4 字段5
1 2 3 NUll NULL
结果是:(1+2+3)/3 = 2
NULL的字段不取
...全文
782 17 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
青锋-SS 2008-03-21
  • 打赏
  • 举报
回复
前端的程序也不健壮
青锋-SS 2008-03-21
  • 打赏
  • 举报
回复
[Quote=引用 15 楼 tianzhenbo 的回复:]
引用 13 楼 dobear_0922 的回复:
晕,已经结贴了,,,
如果有一行全为null,就会出现0/0,编译器会报错,sdhylj,dawugui,wzy_love_sly都没考虑这种情况,,,

------------------------------
说的是,结贴了 不好意思 要不再开个贴把分给你 呵呵
[/Quote]这样的记录一般情况下不是存在的,如果这样的话,数据库设计就有问题了.
tianzhenbo 2008-03-21
  • 打赏
  • 举报
回复
[Quote=引用 13 楼 dobear_0922 的回复:]
晕,已经结贴了,,,
如果有一行全为null,就会出现0/0,编译器会报错,sdhylj,dawugui,wzy_love_sly都没考虑这种情况,,,
[/Quote]
------------------------------
说的是,结贴了 不好意思 要不再开个贴把分给你 呵呵
dobear_0922 2008-03-21
  • 打赏
  • 举报
回复
create table tb(字段1 int, 字段2 int, 字段3 int, 字段4 int, 字段5 int)
insert tb select 1, 2, 3, NUll, NULL
union all select 1,2,3,4,5
union all select null,null,null,null,null

select case (isnull(字段1,0)+isnull(字段2,0)+isnull(字段3,0)+isnull(字段4,0)+isnull(字段5,0))
when 0 then 0
else (isnull(字段1,0)+isnull(字段2,0)+isnull(字段3,0)+isnull(字段4,0)+isnull(字段5,0))
/(isnumeric(字段1)+isnumeric(字段2)+isnumeric(字段3)+isnumeric(字段4)+isnumeric(字段5))
end as average
from tb

/*
average
-----------
2
3
0

(3 row(s) affected)
*/

drop table tb
dobear_0922 2008-03-21
  • 打赏
  • 举报
回复
晕,已经结贴了,,,
如果有一行全为null,就会出现0/0,编译器会报错,sdhylj,dawugui,wzy_love_sly都没考虑这种情况,,,
wobencm 2008-03-21
  • 打赏
  • 举报
回复
呵呵,结帖...
dobear_0922 2008-03-21
  • 打赏
  • 举报
回复
--try
create table tb(字段1 int, 字段2 int, 字段3 int, 字段4 int, 字段5 int)
insert tb select 1, 2, 3, NUll, NULL
union all select 1,2,3,4,5
union all select null,null,null,null,null

select case (isnull(字段1,0)+isnull(字段2,0)+isnull(字段3,0)+isnull(字段4,0)+isnull(字段5,0))
when 0 then 0
else (isnull(字段1,0)+isnull(字段2,0)+isnull(字段3,0)+isnull(字段4,0)+isnull(字段5,0))
/(5-isnull(nullif(isnull(字段1,1), 字段1), 0)-isnull(nullif(isnull(字段2,1), 字段2), 0)
-isnull(nullif(isnull(字段3,1), 字段3), 0)-isnull(nullif(isnull(字段4,1), 字段4), 0)
-isnull(nullif(isnull(字段5,1), 字段5), 0))
end as average
from tb

/*
average
-----------
2
3
0

(3 row(s) affected)
*/

drop table tb
tianzhenbo 2008-03-21
  • 打赏
  • 举报
回复
不好意思,结贴了没看到你的回复
tianzhenbo 2008-03-21
  • 打赏
  • 举报
回复
谢谢 大家 结贴了 呵呵
糊涂了忘了case when 了 哈哈!!
dawugui 2008-03-21
  • 打赏
  • 举报
回复
create table tb(字段1 int, 字段2 int, 字段3 int , 字段4 int , 字段5 int)
insert into tb values( 1 , 2 , 3 , NUll, NULL )

select myavg =
(isnull(字段1,0) + isnull(字段2,0) + isnull(字段3,0) + isnull(字段4,0) + isnull(字段5,0))/
(case when 字段1 is not null then 1 else 0 end +
case when 字段2 is not null then 1 else 0 end +
case when 字段3 is not null then 1 else 0 end +
case when 字段4 is not null then 1 else 0 end +
case when 字段5 is not null then 1 else 0 end)
from tb

drop table tb

/*
myavg
-----------
2

(所影响的行数为 1 行)
*/
wzy_love_sly 2008-03-21
  • 打赏
  • 举报
回复
declare @tb table (字段1 int,字段2 int,字段3 int,字段4 int,字段5 int)
insert into @tb select 1,2,3,null,null

select
sum(isnull(字段1,0)+isnull(字段2,0)+isnull(字段3,0)+isnull(字段4,0)+isnull(字段5,0))/
sum(case when 字段1 is null then 0 else 1 end +
case when 字段2 is null then 0 else 1 end +
case when 字段3 is null then 0 else 1 end +
case when 字段4 is null then 0 else 1 end +
case when 字段5 is null then 0 else 1 end ) as '平均数'
from @tb


tianzhenbo 2008-03-21
  • 打赏
  • 举报
回复
奥,这样好像可以 呵呵,谢谢了!
utpcb 2008-03-21
  • 打赏
  • 举报
回复
select isnull(col1,0)+isnull(col2,0)+isnull(col3,0)+isnull(col4,0)+isnull(col5,0)/(case when col1 is null then 0 else 1 end + case when col2 is null then 0 else 1 end + case when col3 is null then 0 else 1 end + case when col4 is null then 0 else 1 end + case when col4 is null then 0 else 1 end + case when col5 is null then 0 else 1 end)
就这么办
dawugui 2008-03-21
  • 打赏
  • 举报
回复
求一行的平均值格式如下:
字段1 字段2 字段3 字段4 字段5
1 2 3 NUll NULL
结果是:(1+2+3)/3 = 2
NULL的字段不取
----

select
(isnull(字段1,0) + isnull(字段2,0) + isnull(字段3,0) + isnull(字段4,0) + isnull(字段5,0))/
(case when 字段1 is not null then 1 else 0 end +
case when 字段2 is not null then 1 else 0 end +
case when 字段3 is not null then 1 else 0 end +
case when 字段4 is not null then 1 else 0 end +
case when 字段5 is not null then 1 else 0 end)
from tb
tianzhenbo 2008-03-21
  • 打赏
  • 举报
回复
恩,不知道分母怎么弄
青锋-SS 2008-03-21
  • 打赏
  • 举报
回复
select isnull(col1,0)+isnull(col2,0)+isnull(col3,0)+isnull(col4,0)+isnull(col5,0)/(case when col1 is null then 0 else 1 end + case when col2 is null then 0 else 1 end + case when col3 is null then 0 else 1 end + case when col4 is null then 0 else 1 end + case when col4 is null then 0 else 1 end + case when col5 is null then 0 else 1 end)
青锋-SS 2008-03-21
  • 打赏
  • 举报
回复
貌似有些难度
这样就把分母不好弄
select isnull(col1,0)+isnull(col2,0)+isnull(col3,0)+isnull(col4,0)+isnull(col5,0)/?

34,838

社区成员

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

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