Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/74.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 将文本转换为数字以进行客户满意度调查_Sql_Sql Server - Fatal编程技术网

Sql 将文本转换为数字以进行客户满意度调查

Sql 将文本转换为数字以进行客户满意度调查,sql,sql-server,Sql,Sql Server,我有客户满意度反馈,如好、坏、优秀等,我想指定一个数值,如优秀=5、好=4、中性=2、不满意=0 然后按类别对值求和 这个查询我只需要以“Select”开头 按月分组 优秀400 好的500用例 select sum(case when satisfaction ='Excellent' then 5 when satisfaction ='Good' then 4 when satisfaction ='Neutral' then 2

我有客户满意度反馈,如好、坏、优秀等,我想指定一个数值,如优秀=5、好=4、中性=2、不满意=0 然后按类别对值求和

这个查询我只需要以“Select”开头

按月分组 优秀400 好的500

用例

select sum(case when satisfaction ='Excellent' then 5 
            when satisfaction ='Good' then 4
            when satisfaction ='Neutral' then 2
             when satisfaction ='Dissatisfied' then 0 end) as satisfaction_val
,category from table_name group by category

我想你应该为此准备一个参考表。如果没有,可以在查询本身中将其放在一起:

select t.category, sum(v.satisfaction_val)
from t cross join
     (values ('Excellent', 5),
             ('Good', 4),
             ('Neutral', 2),
             ('Dissatisfied', 0)
    ) v(satisfaction, satisfaction_val)
    on t.satisfaction = v.satisfaction
group by t.category;
这是一个更好的解决方案,只需在线使用
case
表达式,因为这些值可以重复使用。例如,如果您想要获得肯定回答的总数,您可以执行以下操作:

select t.category, sum(v.satisfaction_val),
       sum(case when v.satisfaction_value > 3 then 1 else 0 end)