如何对两个复杂的SQL查询执行外部联接?
我有两个相对复杂的查询,我正试图将它们合并到一个结果集中 结果集1:如何对两个复杂的SQL查询执行外部联接?,sql,sql-server-2005,outer-join,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
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
我需要在问题id匹配的行上加入它们。请提供帮助。第一个查询中第二个查询中缺少的唯一附加信息是
COUNT(ra.question_option_id) AS TotalAnswers
在dbo.form\u response\u回答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问题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中的行多。我不是专家,但不会:
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
完成工作?您是否尝试过使用CTE或表格变量?好的,我尝试过使用CTE,但没有任何运气。我能够创建一个CTE,但它不允许我做两个。我仍然不知道如何以这种方式实现JOIN。如何处理表变量?这不起作用,因为它会丢失我在第一个查询中的分组,因此计数会返回不同的结果。实际上,重复此操作是可取的,因为我将使用该列计算选择特定响应的次数的百分比。