0

1 つの結果セットに結合しようとしている 2 つの比較的複雑なクエリがあります。

結果セット 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

2番目のクエリから欠落している最初のクエリの唯一の追加情報は

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

もちろん、クエリ2にはクエリ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 に答える