如何在两个复杂的SQL查询上执行OUTER JOIN?

时间:2012-04-10 15:25:19

标签: sql sql-server-2005 outer-join

我有两个相对复杂的查询,我试图将它们连接成一个结果集。

结果集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匹配的行上加入它们。请帮忙。

2 个答案:

答案 0 :(得分:1)

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

COUNT(ra.question_option_id) AS TotalAnswers
<{1>}表上的

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

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

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

所以,我应该插入:

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

现在,当然,这将重复多次,因为查询2中的行多于查询1。

答案 1 :(得分: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

完成工作?