I have tables for questions and answers. Each question can have 0 or multiple answers. When you left join the tables you get something like this:
q1 a1 q1 a2 q2 a3 q3 NULL q4 NULL q5 a4 q6 NULL q7 a5 q7 a6
What I need is the number of answers, and number of questions that have those many answers. i.e.,
number_of_answers number_of_questions_that_have_that_many_answers 0 3 1 2 2 2
What kind of SQL query in Redshift will solve this?
Advertisement
Answer
Another level of GROUP BY
should do it:
SELECT answer_count, COUNT(*) AS number_of_questions FROM ( SELECT q.id, COUNT(a.id) AS answer_count FROM questions q LEFT JOIN answers a ON a.question_id = q.id GROUP BY q.id ) AS x GROUP BY answer_count