テストの質問、それらの質問に対する可能な回答、および可能な各回答がユーザーによって選択された回数を返す次のクエリがあります。
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
私が必要とする唯一のことは、各質問への回答数を合計することですが、私はこれに本当につまずいています. 応答の数を数えて、特定の応答がユーザーによって選択された回数の割合を取得します。
結果セット:
---- ---- ---- -- --------------------------------------------------------------------------- - ------------ ----
1000 1001 1000 10 How many days a week do you drink at least eight glasses (64 oz.) of water? 1 Never 1
1000 1001 1000 10 How many days a week do you drink at least eight glasses (64 oz.) of water? 2 Once 1
1000 1001 1000 10 How many days a week do you drink at least eight glasses (64 oz.) of water? 3 Two times NULL
1000 1001 1000 10 How many days a week do you drink at least eight glasses (64 oz.) of water? 4 Three times 2
1000 1001 1000 10 How many days a week do you drink at least eight glasses (64 oz.) of water? 5 Four times NULL
1000 1001 1000 10 How many days a week do you drink at least eight glasses (64 oz.) of water? 6 Five or more NULL