我有两个相对复杂的查询,我试图将它们合并到一个结果集中。
结果集 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 匹配的行。请帮忙。