如何知道两个计数值的算术平均值

时间:2019-06-11 10:56:58

标签: sql postgresql

我有用于存储信息的表answers

| EMPLOYEE | QUESTION_ID | QUESTION_TEXT          | SELECTED_OPTION_ID | SELECTED_OPTION_TEXT |
|----------|-------------|------------------------|--------------------|----------------------|
| Mark     | 1           | Do you like soup?      | 1                  | Yes                  |
| Kate     | 1           | Do you like soup?      | 1                  | Yes                  |
| Jone     | 1           | Do you like soup?      | 2                  | No                   |
| Kim      | 1           | Do you like soup?      | 3                  | I don't know         |
| Alex     | 1           | Do you like soup?      | 2                  | No                   |
| Bond     | 1           | Do you like soup?      | 1                  | Yes                  |
| Ford     | 1           | Do you like soup?      | 3                  | I don't know         |
| Mark     | 2           | Do you like ice cream? | 2                  | No                   |
| Kate     | 2           | Do you like ice cream? | 1                  | Yes                  |
| Jone     | 2           | Do you like ice cream? | 1                  | Yes                  |
| Kim      | 2           | Do you like ice cream? | 1                  | Yes                  |
| Alex     | 2           | Do you like ice cream? | 2                  | No                   |
| Bond     | 2           | Do you like ice cream? | 1                  | Yes                  |
| Ford     | 2           | Do you like ice cream? | 3                  | I don't know         |

公式

value_1 = (Number of users who answered "No" or "I don't know" to the first question) / (The total number of people who answered to the first question)

value_2 = (Number of users who answered "No" or "I don't know" to the second question) / (The total number of people who answered to the first question)

我可以根据上述公式单独找到值。例如value_1

select
    count(*)
from
    answers
where
    question_id = 1
    and (
        selected_option_id in (2, 3)
        or 
        selected_option_text in ('No', 'I don\'t know')
    )

我的问题是如何通过一个sql查询正确算出这2个值的值?

换句话说,我需要找到平均值: enter image description here

3 个答案:

答案 0 :(得分:1)

您在寻找下面的东西吗?

SELECT 
SUM(CASE  WHEN QUESTION_ID = 1 AND SELECTED_OPTION_TEXT  <> 'Yes' THEN 1 ELSE 0 END)
/
SUM(CASE  WHEN QUESTION_ID = 1 THEN 1 ELSE 0 END) value_1 ,
SUM(CASE  WHEN QUESTION_ID = 2 AND SELECTED_OPTION_TEXT  <> 'Yes' THEN 1 ELSE 0 END)
/
SUM(CASE  WHEN QUESTION_ID = 2 THEN 1 ELSE 0 END) value_2
FROM answers 

要获取平均值,请使用以下脚本-

SELECT (A.value_1+A.value_2)/2.0 
FROM
(
    SELECT 
    SUM(CASE  WHEN QUESTION_ID = 1 AND SELECTED_OPTION_TEXT  <> 'Yes' THEN 1 ELSE 0 END)*1.0
    /
    SUM(CASE  WHEN QUESTION_ID = 1 THEN 1 ELSE 0 END)*1.0 value_1 ,
    SUM(CASE  WHEN QUESTION_ID = 2 AND SELECTED_OPTION_TEXT  <> 'Yes' THEN 1 ELSE 0 END)*1.0
    /
    SUM(CASE  WHEN QUESTION_ID = 2 THEN 1 ELSE 0 END)*1.0 value_2
    FROM answers
)A

答案 1 :(得分:1)

您可以使用条件总和

select  (sum( case when QUESTION_ID = 1 AND  
        SELECTED_OPTION_ID in ( 2,3) THEN 1 else 0 end )::float /  
      sum( case when QUESTION_ID = 1 then 1 else 0 end)::float )*100 first_question_rate,
    (sum( case when QUESTION_ID = 2 AND  
        SELECTED_OPTION_ID in ( 2,3) THEN 1 else 0 end )::float /  
      sum( case when QUESTION_ID = 2 then 1 else 0 end)::float)*100 second_question_rate,
    (( sum( case when QUESTION_ID = 1 AND  SELECTED_OPTION_ID in ( 2,3) THEN 1 else 0 end )::float /  
      sum( case when QUESTION_ID = 1 then 1 else 0 end)::float +
      sum( case when QUESTION_ID = 2 AND  SELECTED_OPTION_ID in ( 2,3) THEN 1 else 0 end )::float /  
      sum( case when QUESTION_ID = 2 then 1 else 0 end) ::float)/2)*100 avg 
from answer 

答案 2 :(得分:0)

我很确定您想要条件聚合。我怀疑你想要

select question_id,
       count(*) filter (where selected_option_id in (2, 3)) as num_2_3,
       avg( selected_option_id in (2, 3)::int ) as ratio_2_3
from answers
group by question_id;   

对于每个问题,它提供的答案数为2或3,以及这些答案与所有​​答案的比率。