Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The only additional piece of information in the 1st query that's missing from the second is</p> <pre><code>COUNT(ra.question_option_id) AS TotalAnswers </code></pre> <p>on the <code>dbo.form_response_answers ra</code> table.</p> <p>So, just add this count into your select:</p> <pre><code> (select count(*) from dbo.form_response_answers ra where qo.question_option_id = ra.question_option_id) as AS TotalAnswers </code></pre> <p>as in:</p> <pre><code>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 </code></pre> <hr> <p>EDIT: You wanted the total # of Answers for each sq.question_id.</p> <p>So, I should have inserted:</p> <pre><code>(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 </code></pre> <p>Now, of course, that will be repeated multiple times because there are more rows in Query 2 than Query 1.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload