0

I have the following table and would like to get the result as count

Users   QuestionType    Question    Answer
User 1  Employment      Question 1      Yes
                        Question2       NULL

User 1  Job History     Question 3      Yes
                        Question 4      Yes
                        Question 5      No

User 1  Work Status     Question 6      null
                        Question 7      null

User 2  Job History     Question 3      Yes
                        Question 4      Yes
                        Question 5      No

User 2  Work Status     Question 6      yes
                        Question 7      yes

Result

            Count of Users
Employment      1
Job History     2
Work Status     1

How do I get a single count for multiple rows from within mysql?

7
  • 1
    I don't understand your results. Why would the count for Pet be 1 and not 2, or Wild be 3? If that's what you want, just use COUNT(Animal) as Count GROUP BY TYPE in your query. Commented May 4, 2020 at 16:33
  • and where does Others come from ? Haven't you tried anything to get those results ? Commented May 4, 2020 at 16:36
  • He seems to want a count of 1 even if it's more than 1 ("count as single with multiple rows"), but then what's the point of even having count? It's just a constant 1. Commented May 4, 2020 at 16:36
  • I simplified the example. we have multiple questions that needs to be asked in questionnaire. If the user selects any one value then we need to count it as 1. For e.g. If the user answers to Question1 or Question2 then we need to count 1 for an aggregate report. If the user did not answer any of the Question - then it is 0. Commented May 4, 2020 at 16:37
  • Update the example - hope this makes sense Commented May 4, 2020 at 16:44

2 Answers 2

1

This will give us a list of question types

SELECT DISTINCT QUESTION_TYPE 
FROM TABLE_NAME

This will give us a count of the number of questions of a given type

SELECT QUESTION_TYPE, COUNT(*) AS COUNT
FROM TABLE_NAME
WHERE ANSWER IS NOT NULL
GROUP BY QUESTION_TYPE

Now we can join them together.

SELECT BASE.QUESTION_TYPE, COALESCE(CNT.COUNT,0) AS COUNT
FROM (
  SELECT DISTINCT QUESTION_TYPE 
  FROM TABLE_NAME
) AS BASE
LEFT JOIN (
  SELECT QUESTION_TYPE, COUNT(*) AS COUNT
  FROM TABLE_NAME
  WHERE ANSWER IS NOT NULL
  GROUP BY QUESTION_TYPE
) CNT ON BASE.QUESTION_TYPE = CNT.QUESTION_TYPE

That give a count of answered questions, if you want a "count of users" then you have to change count to look like this:

SELECT QUESTION_TYPE, COUNT(USERS) AS COUNT
FROM TABLE_NAME
WHERE ANSWER IS NOT NULL
GROUP BY QUESTION_TYPE

This answer is more complicated then a simple group by because there is a potential for there to be question types that no one has answered. If that never happens then use Gordan's answer.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you - I did create another table to use it as reference and was able to do a subquery with join and use the entire query as another subquery for the counts
0

If you want a count of users on non-NULL answers:

select questiontype, count(distinct user_id)
from t
where answer is not null
group by questiontype;

1 Comment

this won't get zero for question types with all nulls

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.