我正在尝试使用汇总,并想知道是否有一种方法来实现这一点,而不使用子查询,CTE或临时表。这是我的密码:
select coalesce(Answer,case Grouping_ID(Answer) when 1 then 'Total' end) as Answer
, Count(*) as CSAT_Count
, Case Answer
when 'Average' then 2*count(*)
when 'Outstanding' then 3*Count(*)
when 'Unsatisfactory' then 1*Count(*)
end as CSAT_Score
from CSAT_Table
Where Empl_ID = 98
Group by Answer
with rollup结果如下:
Answer CSAT_Count CSAT_Score
----------------------------------------------
Average 13 26
Outstanding 126 378
Unstatisfactory 6 6
Total 145 NULL我想让CSAT_Score中的空返回反映分数的总和,但是我在想,因为分数依赖于计数(*)来计算,所以我不能让汇总来帮我做到这一点,因为我不能按集合进行分组。
就像我前面说的,我知道一个子查询,CTE或Temp表存储计数的结果可以工作,但是由于我刚开始使用Rollup,我想看看是否还有什么可以用这个函数来实现这个功能。
谢谢
发布于 2018-03-22 16:11:28
这个版本的脚本使用rollup。(顺便说一句,它产生了同样的结果)
select coalesce(Answer,case Grouping_ID(Answer) when 1 then 'Total' end) as Answer
, Count(*) as CSAT_Count
, Sum(Case Answer
when 'Average' then 2
when 'Outstanding' then 3
when 'Unsatisfactory' then 1
end) as CSAT_Score
from CSAT_Table
Where Empl_ID = 98
Group by Answer
with rolluphttps://stackoverflow.com/questions/49433099
复制相似问题