sql 取平均分的sql语句

yuna@liu 2011-07-11 07:08:39
有作品表 3个分数字段 core_1 ,core_2, core_3 (3个评委的打分)

如果core_1值为0 表示评委还未给作品打分。core_2, core_3 同理。
要求:
比如如果该作品 core_1不为0,core_2不为0,core_3为0
则取core_1,core_2出来算平均分。如果都为0,则作品得分也为0。这样的规则算平均分。

现在查处表中所有作品的得分,求最快速算分的sql语句:

select id, (core_1+core_2+core_3),
(case when core_1=0 then 0 else 1 end)+
(case when core_2=0 then 0 else 1 end)+
(case when core_3=0 then 0 else 1 end)
from [表名]


这样算但是报错提示被除数不能为0
...全文
90 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
qgqch2008 2011-07-11
  • 打赏
  • 举报
回复
select id, (core_1+core_2+core_3) as [sum],
case when core_1+core_2+core_3=0 then 0 else (core_1+core_2+core_3)/(sign(core_1)+sign(core_2)+sign(core_3)) end as [average]
from [表名]
暖枫无敌 2011-07-11
  • 打赏
  • 举报
回复

select id,isnull(core_1,0)+isnull(core_2,0)+isnull(core_3,0) as 总分
,(isnull(core_1,0)+isnull(core_2,0)+isnull(core_3,0))/(case when core_1 is null then 0 else 1 end +case when core_2 is null then 0 else 1 end +case when core_3 is null then 0 else 1 end) as 平均分
from [表名];

17,740

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 .NET Framework
社区管理员
  • .NET Framework社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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