SQL - 响应总数中的响应百分比,按国家/地区分组

时间:2017-03-01 17:21:55

标签: sql sql-server

所以国家问题,其答案。我想要从该问题的所有答案中选择一个特定问题的答案的百分比,按国家/地区分组。

请注意,每个国家/地区都有多个相同问题的实例,每个实例都有不同数量的个别答案。还有一个字段包含每个答案/条目的total_nr_responses。

样本数据

question_id   country    answer_key   total_nr_responses
A1            Austria    A1_B1        3
A1            Austria    A1_B1        0
A1            Austria    A1_B2        4
A1            Belgium    A1_B1        4
A1            Belgium    A1_B1        10
A2            Austria    A2_B1        2
...

问题A1的预期结果,答案A1_B1为每个国家(100x3 / 7)总答复中特定答案的total_nr_responses的百分比:

 Country   Result
 Austria   percentage
 Belgium   percentage

我试过这样的事情,但我不知道如何获得每个国家的百分比/如何在每个国家/地区的子查询中进行分组,以便整个查询有效:

Select Country, count(total_nr_responses)* 100 / (Select count(total_nr_responses) From my_table WHERE question_key = 'A1') as percentage
From my_table
WHERE question_id = 'A1' AND answer_key = 'A1_B1'
GROUP BY Country

任何帮助都非常感激。

4 个答案:

答案 0 :(得分:0)

也许这就是你要找的东西?

SELECT
  mt.country,
  SUM(mt.total_nr_responses) * 100 / p.total_sum_responses
FROM
  my_table AS mt,
  ( SELECT country, SUM(total_nr_responses) AS total_sum_responses FROM my_table WHERE question_id = 'A1' GROUP BY country ) AS p
WHERE
  question_id = 'A1' AND
  answer_key = 'A1_B1' AND
  p.country = mt.country
GROUP BY
  mt.country,
  p.total_sum_responses

由于计算百分比,我无法使其与OVER(PARTITION BY)一起使用。很高兴看到Cade Roux在代码中完全阐述的内容。

嵌套SELECT和CROSS APPLY之间的执行计划非常相似,并且所有三个(窗口函数,交叉应用和嵌套选择)产生类似的结果。如果处理大量数据,请确保您拥有该国家/地区的综合索引和question_id。很高兴看到同样问题的各种解决方案!

答案 1 :(得分:0)

如何使用CROSS APPLY获取总数?

<强>查询

SELECT mt.question_id, mt.country, mt.answer_key, (SUM(mt.total_nr_responses) * 100 / ca.total_nr_responses) AS result
FROM my_table mt
CROSS APPLY (SELECT SUM(total_nr_responses) AS total_nr_responses
             FROM my_table
             WHERE question_id = mt.question_id AND country = mt.country) ca
WHERE mt.question_id = 'A1' AND mt.answer_key = 'A1_B1'
GROUP BY mt.question_id, mt.country, mt.answer_key, ca.total_nr_responses

<强>输出

+-------------+---------+------------+--------+
| question_id | country | answer_key | result |
+-------------+---------+------------+--------+
|     A1      | Austria |    A1_B1   |   42   |
|     A1      | Belgium |    A1_B1   |  100   |
+-------------+---------+------------+--------+

答案 2 :(得分:0)

您可以将SUM函数与窗口规范一起使用。

select distinct country,
question_id,
answer_key,
100.0*sum(total_nr_responses) over(partition by country,question_id,answer_key)/
sum(total_nr_responses) over(partition by country,question_id) as pct
from my_table

如果需要,添加where子句以将结果限制为特定问题/答案/国家/地区。

答案 3 :(得分:0)

通常,您可以使用简单的窗口函数和聚合来执行此操作:

Select Country,
       count(total_nr_responses) * 100 / sum(count(total_nr_responses)) over () as percentage
From my_table
where question_id = 'A1' AND answer_key = 'A1_B1'
group by Country;

注意:SQL Server执行整数除法。我会将100更改为100.0并在分割后格式化结果。否则,这些值不会接近最多100个。