1

I have a table called "questionnaire" which has the following structure:

user_id   |      Q01      |       Q02        |      Q03         |
00001     |      Yes      |      Yes         |      Yes         |
00002     |      Yes      |      No          |      Yes         |
00003     |      Yes      |      No          |      No          |

I am figuring out on how to count the number of "Yes" for each User (user_id). For example:

00001 - 3
00002 - 2
00003 - 1

I just want to display the results without the UserID..

Before this, I have stored the Yes and No as "1" and "0", therefor I could use the following:

SELECT CONCAT(Q01+Q02+Q03) FROM `#__table` WHERE `id` = '[user_id]'

I cant seem to find the right query since its no longer an integer..

2
  • you mess up with table structure. Commented Oct 24, 2013 at 3:58
  • A database table is NOT a spreadsheet. Normalize your data. Commented Oct 24, 2013 at 8:25

3 Answers 3

1
SELECT (Q01 = 'Yes') + (Q02 = 'Yes') + (Q03 = 'Yes')
FROM #__table 
WHERE id = '[user_id]'
Sign up to request clarification or add additional context in comments.

2 Comments

could you tell me how to do the same in sql-server?
This is the one I am finding.. short and simple!
0
SELECT if(Q01 = 'Yes',1,0) + if(Q02 = 'Yes',1,0) + if(Q03 = 'Yes',1,0)
FROM #__table 
WHERE id = '[user_id]'

Comments

0
SELECT sum(yes)
FROM (
        (SELECT count(*) yes
         FROM #__TABLE
         WHERE Q01='yes'
           OR Q02='yes'
           OR Q03='yes'
           AND user_id=00001)
      UNION ALL
        (SELECT count(*) yes
         FROM #__TABLE
         WHERE Q01='yes'
           OR Q02='yes'
           OR Q03='yes'
           AND user_id=00002)
      UNION ALL
        (SELECT count(*) yes
         FROM #__TABLE
         WHERE Q01='yes'
           OR Q02='yes'
           OR Q03='yes'
           AND user_id=00003)) AS tt;

i think this will work fine.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.