27,581
社区成员
发帖
与我相关
我的任务
分享
CREATE TABLE test
(
id VARCHAR(20) ,
name VARCHAR(20) ,
score INT ,
score1 INT ,
score2 INT ,
score3 INT
)
INSERT INTO test
SELECT 1 ,
'a' ,
10 ,
8 ,
NULL ,
9
SELECT (ISNULL(score, 0) + ISNULL(score1, 0) + ISNULL(score2, 0)
+ ISNULL(score3, 0))*1.0 / (CASE WHEN score IS NULL THEN 0
ELSE 1
END + CASE WHEN score1 IS NULL THEN 0
ELSE 1
END + CASE WHEN score2 IS NULL THEN 0
ELSE 1
END
+ CASE WHEN score3 IS NULL THEN 0
ELSE 1
END)
FROM test
select isnull(score,0)+isnull(score1,0)+isnull(score2,0)+isnull(score3,0)/
(case when score is null then 0 else 1 end)+
(case when score1 is null then 0 else 1 end)+
(case when score2 is null then 0 else 1 end)+
(case when score3 is null then 0 else 1 end)+
from tb