Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7766899
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T15:32:24+00:00 2026-06-01T15:32:24+00:00

I have two relatively complex queries that I am trying to join together into

  • 0

I have two relatively complex queries that I am trying to join together into one result set.

Result set 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

Result set 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

I need to join them on the rows where question_id matches. Please help.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T15:32:26+00:00Added an answer on June 1, 2026 at 3:32 pm

    The only additional piece of information in the 1st query that’s missing from the second is

    COUNT(ra.question_option_id) AS TotalAnswers
    

    on the dbo.form_response_answers ra table.

    So, just add this count into your select:

        (select count(*) from dbo.form_response_answers ra
             where qo.question_option_id = ra.question_option_id) as AS TotalAnswers
    

    as in:

    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
    

    EDIT: You wanted the total # of Answers for each sq.question_id.

    So, I should have inserted:

    (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
    

    Now, of course, that will be repeated multiple times because there are more rows in Query 2 than Query 1.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to join two relatively simple tables together, but my query is experiencing
I have a relatively simple algorithm that walks an std::vector looking for two neighbouring
I have two functions that make $.getJSON calls - one looks at JSON stored
I am using Spring 3.0.2. I have two relatively simple bean definitions. One has
I have two Doctrine entities that have a one-to-many relationship, like this: License class
I have two string lists that I'm working with. One that has a list
I have a relatively simple query joining two tables. The Where criteria can be
Have two actionsheet buttons and one modalviewcontroller on mainviewcontroller in application. Now for two
Background I have two services that need to communicate with each other over a
I am relatively new to android. I have two applications A and B. I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.