如何进行正确的计算?

时间:2019-06-18 06:00:35

标签: sql postgresql

在PostgreSQL数据库中,我有一个名为answers的表。该表存储有关用户如何回答问题的信息。表中只有4个问题。同时,回答问题的用户数量可以动态变化,并且用户只能回答部分问题。

answers

| EMPLOYEE | QUESTION_ID | QUESTION_TEXT          | OPTION_ID | OPTION_TEXT  |
|----------|-------------|------------------------|-----------|--------------|
| Bob      | 1           | Do you like soup?      | 1         | Yes          |
| Alex     | 1           | Do you like soup?      | 1         | Yes          |
| Kate     | 1           | Do you like soup?      | 3         | I don't know |
| Bob      | 2           | Do you like ice cream? | 1         | Yes          |
| Alex     | 2           | Do you like ice cream? | 3         | I don't know |
| Oliver   | 2           | Do you like ice cream? | 1         | Yes          |
| Bob      | 3           | Do you like summer?    | 2         | No           |
| Alex     | 3           | Do you like summer?    | 1         | Yes          | 
| Jack     | 3           | Do you like summer?    | 2         | No           |
| Bob      | 4           | Do you like winter?    | 3         | I don't know |
| Alex     | 4           | Do you like winter?    | 1         | Yes          |
| Oliver   | 4           | Do you like winter?    | 3         | I don't know |

我需要这个结果:

| EMPLOYEE | CALC |
|----------|------|
| Bob      | 2    |
| Alex     | 2    |
| Kate     | 1    |
| Jack     | 1    |
| Oliver   | 2    |

calc列由以下公式计算:

CALC = A + B;

A - If a user answered to first and/or second question then the value should be 1, otherwise 0.
B - If a user answered to third and/or fourth question then the value should be 1, otherwise 0.

例如,鲍勃回答了所有4个问题。这就是为什么calc列的Bob值为2的原因。同时,凯特只回答了第一个问题。这就是为什么calc列的Kate值为1的原因。在她的情况下,A为1,B为0。

现在我尝试了这样的代码,但是它没有按我预期的那样工作:

select
    employee,
    (
        case when count(question_id = 1) or count(question_id = 2) > 0 then 1 else 0 end
        +
        case when count(question_id = 3) or count(question_id = 4) > 0 then 1 else 0 end
    ) as calc
from
    answers
group by
    employee

3 个答案:

答案 0 :(得分:1)

您可以尝试使用与<?php header('Content-Type: image/png'); ?> ‰PNG IHDRÈå“š’PLTEÙÚÝ>=<DBAsnjÓÔÛÔÕÛ321‹ŠÐÑØ[XU+*)!! ÕÖÞ //many char here .. etc.... 不同的条件加重函数SUM

查询1

group by

Results

SELECT employee,
       (SUM(DISTINCT CASE WHEN QUESTION_ID IN (1,2) THEN 1 ELSE 0 END) + 
       SUM(DISTINCT CASE WHEN QUESTION_ID IN (3,4) THEN 1 ELSE 0 END)) CALC 
FROM answers
GROUP BY employee

答案 1 :(得分:1)

类似于D-Shih的答案,也可以通过使用过滤器进行计数来实现

select
    employee,
    (
        case
            when count(question_id) filter (where question_id in(1, 2)) > 0
            then 1
            else 0
        end +
        case
            when count(question_id) filter (where question_id in(3, 4)) > 0
            then 1
            else 0
        end
    ) as calc
from answers
group by employee
order by employee

答案 2 :(得分:1)

在Postgres中,我将其表示为条件聚合,但不使用COUNT(DISTINCT)

select employee,
       (max( (question_id in (1, 2))::int ) +
        max( (question_id in (3, 4))::int )
       ) as calc
from answers
group by employee;

除了更加简洁之外,count(distinct)通常比min()max()count()和{{1 }}。