0

我有两个相对复杂的查询,我试图将它们合并到一个结果集中。

结果集 1:

SELECT  sq.question_id,     
    COUNT(ra.question_option_id) AS TotalAnswers

FROM    dbo.survey_question sq
    LEFT OUTER JOIN dbo.question_option qo
        ON sq.question_id = qo.question_id
    LEFT OUTER JOIN dbo.form_response_answers ra
        ON qo.question_option_id = ra.question_option_id
GROUP BY sq.question_id

结果集 2:

SELECT  p.program_id, 
    p.pre_survey_form_id, 
    p.post_survey_form_id, 
    fq.form_id, 
    sq.question_id, 
    sq.question_text, 
    qo.question_option_id, 
    qo.option_text, 
    G.Total

FROM    dbo.program p
    LEFT OUTER JOIN dbo.form_question fq
        ON p.pre_survey_form_id = fq.form_id OR p.post_survey_form_id = fq.form_id
    LEFT OUTER JOIN dbo.survey_question sq
        ON fq.question_id = sq.question_id
    LEFT OUTER JOIN dbo.question_option qo 
        ON sq.question_id = qo.question_id
    LEFT OUTER JOIN (
        SELECT ra.question_id, ra.question_option_id, COUNT(*) AS Total
        FROM dbo.form_response_answers ra
        GROUP BY ra.question_option_id, ra.question_id 
    ) G
        ON G.question_id = sq.question_id AND G.question_option_id = qo.question_option_id

ORDER BY p.program_id, fq.form_id, sq.question_id, qo.question_option_id

我需要将它们加入 question_id 匹配的行。请帮忙。

4

2 回答 2

1

第二个查询中缺少的第一个查询中唯一的附加信息是

COUNT(ra.question_option_id) AS TotalAnswers

dbo.form_response_answers ra桌子上。

因此,只需将此计数添加到您的选择中:

    (select count(*) from dbo.form_response_answers ra
         where qo.question_option_id = ra.question_option_id) as AS TotalAnswers

如:

SELECT  p.program_id, 
        p.pre_survey_form_id, 
        p.post_survey_form_id, 
        fq.form_id, 
        sq.question_id, 
        sq.question_text, 
        qo.question_option_id, 
        qo.option_text, 
        G.Total,
        (select count(*) from dbo.form_response_answers ra
         where qo.question_option_id = ra.question_option_id) as AS TotalAnswers


FROM    dbo.program p
    LEFT OUTER JOIN dbo.form_question fq
    ON p.pre_survey_form_id = fq.form_id OR p.post_survey_form_id = fq.form_id
LEFT OUTER JOIN dbo.survey_question sq
    ON fq.question_id = sq.question_id
LEFT OUTER JOIN dbo.question_option qo 
    ON sq.question_id = qo.question_id
LEFT OUTER JOIN (
    SELECT ra.question_id, ra.question_option_id, COUNT(*) AS Total
    FROM dbo.form_response_answers ra
    GROUP BY ra.question_option_id, ra.question_id 
) G
    ON G.question_id = sq.question_id AND G.question_option_id = qo.question_option_id

ORDER BY p.program_id, fq.form_id, sq.question_id, qo.question_option_id

编辑:您想要每个 sq.question_id 的总答案数。

所以,我应该插入:

(select count(ra2.question_option_id) 
   from dbo.question_option qo2
   LEFT OUTER JOIN dbo.form_response_answers ra2
       ON qo2.question_option_id = ra2.question_option_id
   where qo2.question_id = sq.question_id) as TotalAnswers

现在,当然,这将重复多次,因为 Query 2 中的行数比 Query 1 多。

于 2012-04-10T15:32:37.787 回答
0

我不是专家,但不会:

SELECT  sq.question_id,     
    COUNT(ra.question_option_id) AS TotalAnswers INTO [#temp_table1]
...

SELECT  p.program_id, 
    p.pre_survey_form_id, 
    p.post_survey_form_id, 
    fq.form_id, 
    sq.question_id, 
    sq.question_text, 
    qo.question_option_id, 
    qo.option_text, 
    G.Total
INTO [#temp_table2]
...

然后:

SELECT * FROM [#temp_table1] JOIN ON [#temp_table1].question_id = [#temp_table2].question_id

把事做好?

于 2012-04-10T16:08:55.570 回答