My problem is: I have a query that returns this table:
Explaining the table: it shows some questions and their numerical answers (the number 0 can mean No, the number 1 can mean Yes, the number 2 can mean a little and so on) What I want is this table to become this:
So it brings me how many answers in each answer option I had. However, the number of response options varies. In this example I have options for 0 to 4, but there are cases where the options go from 0 to 10. So, how can I create columns for each of these options?
My query is something like this:
SELECT
data,
id,
question,
answer,
COUNT(*) AS info2,
COUNT(CASE WHEN answer IS NOT NULL THEN 1 END) AS info3,
COUNT(CASE WHEN answer IS NULL THEN 1 END) AS info4
FROM table1
JOIN table2
ON table1.id = table2.id
WHERE table1.variable_y BETWEEN '2020-01-01 00:00:00' AND '2020-05-31 23:59:59'
AND variable_x = 'ABC123'
AND variable_z = 'ABC'
GROUP BY 1,2,3,4