#每条记录有个平均分,平均分是这样算的,如果评分为0,表明没人给打分,则不计入平均分之列

Gooing 2004-08-02 10:21:42
我的一个视图是这样的
人员id,项目p,评分s1,评分s2,评分s3,评分s4

有如下的记录:
1> 小G,跳水, 5,5,5,3
2> 小G,跳高, 5,4,6,0
3> 大G,跳水,0,0,5,5
......

每条记录应该有个平均分,平均分是这样算的,如果评分为0,表明没人给打分,则不计入平均分之列,如1>是4.5 ,2>是(5+4+6)/3 =5,
而不是(5+4+6+0)/4 = 3.75;最后按人员将平均分的总和给算出来,如:
小G,9.5
大G,5
......

这样的SQL怎么写?我该如何判断 /几 ?
谢谢!
...全文
186 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
yjr332533 2004-08-02
  • 打赏
  • 举报
回复
select id,
p,
case when s1+s2+s3+s4=0
then 0
else (s1+s2+s3+s4)/((sign(isnull(s1,0))+sign(isnull(s2,0))+sign(isnull(s3,0))+sign(isnull(s4,0)))*1.0)
end as 平均分
from table

wozhuchuanwei 2004-08-02
  • 打赏
  • 举报
回复

Create Table #TB (
id int ,p VarChar(10), s1 int, s2 int, s3 int, s4 int)
INSERT INTO #TB (id,p,s1,s2,s3,s4)
Select 1,'跳水',5,5,5,3 UNION ALL
Select 2,'跳高',5,4,6,0 UNION ALL
Select 3,'跳水',0,0,5,5

Select * FROM #TB
SELECT *,CAST((s1+s2+s3+s4) As decimal(9,3))/(CASE s1 when 0 then 0 Else 1 END+CASE s2 when 0 then 0 Else 1 END+CASE s3 when 0 then 0 Else 1 END+CASE s4 when 0 then 0 Else 1 END) as 平均分 FROM #TB
Drop Table #TB


(所影响的行数为 3 行)

id p s1 s2 s3 s4
----------- ---------- ----------- ----------- ----------- -----------
1 跳水 5 5 5 3
2 跳高 5 4 6 0
3 跳水 0 0 5 5

(所影响的行数为 3 行)

id p s1 s2 s3 s4 平均分
----------- ---------- ----------- ----------- ----------- ----------- ----------------------
1 跳水 5 5 5 3 4.50000000000000
2 跳高 5 4 6 0 5.00000000000000
3 跳水 0 0 5 5 5.00000000000000

(所影响的行数为 3 行)

zjcxc 2004-08-02
  • 打赏
  • 举报
回复
select 人员id,项目p
,平均分=case (isnull(评分s1,0)+isnull(评分s2,0)+isnull(评分s3,0)+isnull(评分s4,0))
when 0 then 0
else (isnull(评分s1,0)+isnull(评分s2,0)+isnull(评分s3,0)+isnull(评分s4,0))
/(case isnull(评分s1,0) when 0 then 0 else 1 end
+case isnull(评分s2,0) when 0 then 0 else 1 end
+case isnull(评分s3,0) when 0 then 0 else 1 end
+case isnull(评分s4,0) when 0 then 0 else 1 end)
end
from 表
victorycyz 2004-08-02
  • 打赏
  • 举报
回复
select id,
p,
case when s1+s2+s3+s4=0
then 0
else (s1+s2+s3+s4)/((sign(s1)+sign(s2)+sign(s3)+sign(s4))*1.0)
end as 平均分
from tablename

victorycyz 2004-08-02
  • 打赏
  • 举报
回复
select id,
p,
case when s1+s2+s3+s4=0
then 0
else (s1+s2+s3+s4)/(sign(s1)+sign(s2)+sign(s3)+sign(s4))
end as 平均分
from tablename

Gooing 2004-08-02
  • 打赏
  • 举报
回复
thank you all!

27,580

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 应用实例
社区管理员
  • 应用实例社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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