1. We have moved from vBulletin to XenForo and you are viewing the site in the middle of the move. Though the functional aspect of everything is working fine, we are still working on other changes including the new design on Xenforo.
    Dismiss Notice

Group By Query - Count Multiple Choice Answers

Discussion in 'Database' started by jklose, Mar 12, 2010.

  1. jklose

    jklose New Member

    Joined:
    Mar 12, 2010
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    I'm trying to count answers to a series of multiple questions but can't get the SQL query to work properly.

    I have a table as follows:
    Code:
    Q1 | Q2 | Q3
    Agree | Disagree | Strongly Agree
    Disagre | Agree | Agree
    Agree | Strongly Agree | Disagree
    ...
    
    I would like to output totals for each type of response for each question in a (query) table like:
    Code:
    Answer | Q1Count | Q2Count | Q3Count
    Agree | 2 | 3 | 5
    Disagree | 1 | 5 | 3
    Strongly Agree | 4 | 3 | 2
    ...
    
    I've been trying a query like this:
    Code:
    SELECT AnswersTable.Q1, Count(AnswersTable.Q1) AS Q1Count, Count(AnswersTable.Q2) AS Q2Count, Count(AnswersTable.Q3) AS Q3Count
    FROM AnswersTable
    GROUP BY AnswersTable.Q1
    ORDER BY AnswersTable.Q1 DESC;
    
    However, the output from this query is in the wrong order. While the counts for Q1 match the GROUP BY unique values, the other counts are disordered.

    Any tips on how to get the right output? Cheers.
     

Share This Page