【发布时间】:2015-10-09 11:29:33
【问题描述】:
我正在 Postgres 9.3 中创建报告。这是我的SQL Fiddle。
基本上我有两张表,responses和questions,结构是:
responses
->id
->question_id
->response
questions
->id
->question
->costperlead
response 列只能有 3 个值,Yes/No/Possbily,
我的报告应该有以下列:
question_id
, # of Yes Responses
, # of No Responses
, # of Possbily Responses
, Revenue
然后:
# of Yes Responses - count of all Yes values in the response column
# of No Responses - count of all No values in the response column
# of Possbily Responses - count of all 'Possbily' values in the response column
收入是costperlead *(是响应的数量 + 可能响应的数量)。
我不知道如何构建查询,我是新来的,而且我来自 MySQL,所以对于 postgres,有些事情是不同的。在我的 SQL Fiddle 示例中,大多数响应是 Yes 和 Null,最终没关系,会有可能和 No。
到目前为止,我只有:
SELECT a.question_id
FROM responses a
INNER JOIN questions b ON a.question_id = b.id
WHERE a.created_at = '2015-07-17'
GROUP BY a.question_id;
【问题讨论】:
标签: database postgresql count inner-join aggregate-filter