SQL count if columns
问题 What is the best way to create columns which count the number of occurrences of data in a table? The table needs to be grouped by one column. I have seen SELECT sum(CASE WHEN question1 = 0 THEN 1 ELSE 0 END) AS ZERO, sum(CASE WHEN question1 = 1 THEN 1 ELSE 0 END) AS ONE, sum(CASE WHEN question1 = 2 THEN 1 ELSE 0 END) AS TWO, category FROM reviews GROUP BY category where question1 can have a value of either 0, 1 or 2. I have also seen a version of that using count(CASE WHEN question1 = 0 THEN