2017-03-01 61 views
0

所以國家問題具有回答。我想要根據國家分組,從這個問題的所有答案中選出一個具體問題答案的百分比。SQL - 反應的比例在總響應,每個國家分組

請注意,每個國家/地區都有相同問題的多個實例,每個實例的答案不同。還有一個字段包含每個答案/條目的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作爲total_nr_responses特定答案出總響應的百分比,每個國家(100x3/7):

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

對於SQL Server,您將使用OVER(PARTITION BY)在國家/地區組中重置您的號碼。 https://msdn.microsoft.com/en-us/library/ms189461.aspx –

+0

@GordonLinoff謝謝,更新問題 –

回答

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。很高興看到同樣問題的多樣化解決方案!

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 | 
+-------------+---------+------------+--------+ 
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子句將結果限制爲特定的問題/答案/國家(如果需要)。

+0

感謝這個例子。我不喜歡重複的行(例如,如果同一個國家存在多行,question_id和answer_key,但不同的total_nr_responses),但它是窗口函數的一個很好的例子。 – Paurian

+0

忘了添加'distinct' ..謝謝 –

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。