質問の各回答の数を取得するために、いくつかのテーブルにクエリを実行しています。ここで私が行っているのは、調査を行うことです。調査の完了時にいくつかの回答オプションを使用して質問する場合があります。調査の統計を取得する必要があり、次に質問の各回答の数をカウントする必要があります。
これが私のクエリです。
SELECT s.NAME AS surveyname,
COUNT(r.answer_id) AS totalAnswer,
q.id AS questionid,
q.question AS question,
a.answer AS answer,
COUNT(r.textbox) AS totalTextbox,
COUNT(r.textboxmulti) AS totalTextboxmulti,
qt.template AS template,
s.NAME AS surveyname,
COUNT(r.other) AS other
FROM surveys s
INNER JOIN survey_results AS sr
ON s.id = sr.survey_id
INNER JOIN results AS r
ON sr.id = r.surveyresults_id
INNER JOIN questions AS q
ON r.question_id = q.id
INNER JOIN questiontypes AS qt
ON q.questiontype_id = qt.id
LEFT JOIN answers AS a
ON r.answer_id = a.id
WHERE s.id = < cfqueryparam cfsqltype = "cf_sql_integer" value = "#arguments.surveyid#" >
GROUP BY q.id,
a.id
ORDER BY a.rank
このクエリは、私が望むものとまったく同じように機能します。しかし、問題は、ビューに結果を表示しているときに、列名 questionid の cfoutout 属性グループを使用しているにもかかわらず、質問に回答の数が乗算されることです。質問と回答の数の乗算を防ぐにはどうすればよいですか?
これは私が調査結果を表示している方法です
<cfoutput query="rc.data.questions" group="questionid">
<cfswitch expression="#rc.data.questions.template#">
<cfcase value="multiplechoice">
<table class="table table-striped table-hover">
<thead>
<tr>
<th width="50%">#rc.data.questions.question#</th>
<th></th>
<th>
<div class="center">Response Count</div>
</th>
</tr>
</thead>
<cfoutput>
<tbody>
<tr>
<td width="60%">#rc.data.questions.answer#</td>
<td>
<div class="center">#rc.data.questions.totalanswer#</div>
</td>
</tr>
</cfoutput>
<cfif structKeyExists(rc.data.questions, "totalother") AND rc.data.questions.template EQ 'multiplechoiceother' OR rc.data.questions.template EQ 'multiplechoicemultiother'>
<tr>
<td><a href="#buildurl(action='survey.text_other',querystring='id=#questionid#')#" target="_blank">View other Text answers</a></td>
<td><div class="center">#rc.data.questions.totalother#</div></td>
</tr>
</cfif>
</tbody>
</table>
</table>
</cfcase>
</cfswitch>
</cfoutput>