用case语句对一组列求和

时间:2015-10-21 03:40:49

标签: sql sql-server

我有一个SQL语句,它根据值选择一组数据。

Select case when Count(score) >= 2 then 2 else Count(score) end as Count
        from table 
        where questions = answered and score= 3
        group by Surveyid

这打印出以下内容(这是正确的)

enter image description here

我最终需要将所有这些值加在一起。我已经想出了这个,但是我遇到了错误,似乎无法解决问题。

Select Sum(*)
From (   Select case when Count(score) >= 2 then 2 else Count(score) end as Count
            from table 
            where questions = answered and score= 3
            group by Surveyid ) C

2 个答案:

答案 0 :(得分:0)

可能因为Sum(*),请尝试

Select Sum(C.Count)

答案 1 :(得分:0)

这解决了这个问题。

Select Sum(Count)
From (   Select case when Count(score) >= 2 then 2 else Count(score) end as Count
            from table 
            where questions = answered and score= 3
            group by Surveyid ) C