我正在查看上一篇关于"部门使用count"似乎在分开的问题上有问题。下面是我的查询,我得到了结果,但结果是错误的,因为"除以" count(*)
似乎使用了所有商店的数量,而不是商店分组。
我感兴趣的是"除以"还应该基于该特定商店的计数数量。我对分频的查询的第二部分应该是" select count(*) *100 group by stores
"
SELECT store, CAST(((
SUM(CASE WHEN SCORE BETWEEN 9 AND 10 THEN 1 ELSE 0 END) * 1.0 -
SUM(CASE WHEN SCORE BETWEEN 0 AND 6 THEN 1 ELSE 0 END)) / COUNT(*) * 100) AS signed) AS NPS
FROM npsnetsuite
WHERE SCORE IS NOT NULL group by STORE;
示例:
Store A has got total count of 7 rows
Store B has got total count of 12 rows
该部门应该是
XX/7 for Store A
XX/12 for Store B
示例数据:
Date Store Score Case When between 9-10 case when 0-6
18/04/2016 A 9 1
20/04/2016 A 8
26/04/2016 A 10 1
29/04/2016 A 8
30/04/2016 A 10 1
01/05/2016 A 10 1
06/05/2016 A 8
11/05/2016 A 10 1
23/05/2016 A 10 1
04/06/2016 A 6 1
09/06/2016 A 8
08/06/2016 A 10 1
05/04/2016 B 4 1
07/04/2016 B 10 1
18/04/2016 B 10 1
27/04/2016 B 8
01/05/2016 B 9 1
13/05/2016 B 10 1
13/06/2016 B 1 1
14/06/2016 B 6 1
期望输出:
Store A 50
Store B 13
基本上Store A is (7-1)/12 * 100 = 50
。
7
是分数为9 to 10
的情况下的计数,1
是分数0 to 6
的情况下的计数。所以我得到它的第一部分是每个商店的得分数为9到10分数0到6的数量。我需要除以特定商店的计数。如上例所示,商店A包含12行,商店B包含8行。我当前的查询是计算所有商店的数量。
谢谢
答案 0 :(得分:0)
谢谢大家〜最后,设法通过此查询获得上述结果:):
select a.STORE, round((a.count_one / b.count_two) * 100) as final_count from
(select SCORE, STORE, SUM(CASE WHEN SCORE BETWEEN 9 AND 10 THEN 1 ELSE 0 END) * 1.0 -
SUM(CASE WHEN SCORE BETWEEN 0 AND 6 THEN 1 ELSE 0 END) as count_one from npsnetsuite where SCORE IS NOT NULL group by STORE) a,
(select SCORE, STORE, count(*) as count_two from npsnetsuite where SCORE IS NOT NULL group by STORE) b
where a.STORE = b.STORE and a.SCORE IS NOT NULL;