Select 每个问题选项的答案数量(按受访者组)

Select number of answers for each question options by respondent group

让我们使用具有以下结构的 PostgreSQL 数据库:

(所有关系从上到下都是OneToMany

对于具有给定 campaign_id 的“Brandlift”,我们如何检索:

对于每个问题回答*的品牌的数量[=30] =]respondent.segment

*即具有匹配 品牌 ID

anwer_content 行数

这是给定此 dump

所需输出的示例

基本上,对于一个问题

,品牌只是一个可能的answer/choice选项

您可以将 Brandlift 视为多项选择 q,其中每个问题都有相同的选择选项

我用的是你最新的数据,这里是查询。

SELECT
    BQ.id as question_id,
    BR.segment_id,
    BB.name AS Brand_Name,
    COUNT(BAC.brand_id) AS Answer_Count
FROM
    PUBLIC.Brandlift B
    LEFT JOIN brandlift_question BQ ON B.campaign_id = BQ.campaign_id
    LEFT JOIN brandlift_respondent BR ON B.campaign_id = br.campaign_id
    LEFT JOIN brandlift_segment BS ON BR.segment_id = BS.id
    LEFT JOIN brandlift_answer BA ON BQ.id = BA.question_id AND BR.id = BA.respondent_id
    LEFT JOIN brandlift_brand BB ON B.campaign_id = BB.campaign_id
    LEFT JOIN brandlift_answer_content BAC ON BA.id = BAC.answer_id AND BB.id = BAC.brand_id
WHERE
    B.campaign_id = '8d8c79ce-188c-4f5e-bd72-edb854faf34c'
GROUP BY
    BQ.id,
    BB.name,
    BR.segment_id,
    BS.name

你的结果是