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 が一致する行でそれらを結合する必要があります。助けてください。