sql - 3 表加入计算百分比

标签 sql postgresql

我有以下表格:

POLLS
id | name       | colour
-------------------------
1  | first poll | orange
2  | secon poll | blue
3  | third poll | green

QUESTIONS
id | poll_id | what_to_ask
---------------------------
1  | 1       | How nice is stackoverflow?
2  | 1       | Why do you think that?
3  | 2       | What do you like to eat?

CHOICES
id | question_id | choice_text
------------------------------------
1  | 1           | Very nice
2  | 1           | Moderatley nice
3  | 1           | Evil
4  | 2           | Etc.

Answers
id | choice_id | poll_id | question_id
--------------------------------------
1  | 1         | 1       | 1
2  | 1         | 1       | 1
3  | 2         | 1       | 1
4  | 3         | 1       | 1
5  | 1         | 1       | 1

I'm trying to pull back the following:

  • A row for each choice
  • poll information appended to the row (this will duplicate, and it's ok)
  • question information appended to the row (this will duplicate, and it's ok)
  • % of each answer (from all answers for a particular poll)

My query (below) works well if there is an answer per choice, but if there is only one answer (and perhaps 3 choices), it'll only bring back one row.

How can i bring back each answer for a poll, and then the number of answers and % of answers on each choice.

SELECT Count(a.choice_id) AS answer_count,
     q.what_to_ask,
     c.id,
     c.choice_text,
     COALESCE (Count(a.choice_id) * 100.0 / NULLIF((SELECT Count(*)
                                                    FROM   answers
                                                    WHERE  poll_id = 2), 0), 0
     ) AS percentage
FROM   choices c
RIGHT OUTER JOIN answers a
            ON a.choice_id = c.id
INNER JOIN polls p ON p.id = a.poll_id
INNER JOIN questions q ON c.question_id = q.id
WHERE  p.id = 2
GROUP  BY c.id, q.what_to_ask, c.choice_text

最佳答案

我会将选择和答案之间的联接更改为 LEFT JOIN:

SELECT Count(a.choice_id) AS answer_count,
     q.what_to_ask,
     c.id,
     c.choice_text,
     COALESCE (Count(a.choice_id) * 100.0 / NULLIF((SELECT Count(*)
                                                    FROM   answers
                                                    WHERE  poll_id = 2), 0), 0
     ) AS percentage
FROM   choices c
LEFT JOIN answers a
            ON a.choice_id = c.id
INNER JOIN polls p ON p.id = a.poll_id
INNER JOIN questions q ON c.question_id = q.id
WHERE  p.id = 2
GROUP  BY c.id, q.what_to_ask, c.choice_text

在使其成为 RIGHT JOIN 时,您告诉查询仅当答案表中存在匹配项时才返回行。

关于sql - 3 表加入计算百分比,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47842934/

相关文章:

mysql - 如何调用常用的 MySQL 查询

sql - 是否有一个选择性子句可以直接在 PostgreSQL 中注入(inject)选择性,类似于 IBM DB2

mysql - 从两个表在 SQL 中生成报告

mysql - 由另一列分组的所有不同列值的总和?

sql - Postgres : Find position of a specific row within a resultset?

database - 如何自动检测和删除 Postgresql 中重复的外键约束

SQL - min() 获得最低值,max() 获得最高值,如果我想要第二个(或第五个或第 n 个)最低值怎么办?

java - 将三个表内连接到 java DTO

sql - 是否可以编写一个查询来返回两个指定日期之间的每一天的日期?

mysql - Sql 从多个表中选择参数