Sql - count into multiple columns

mysql, sql

Solution

SELECT `num`,
    SUM(type = 1) as `1`, 
    SUM(type = 2) as `2`, 
    SUM(type = 3) as `3`
FROM `your_table`
GROUP BY `num`

Problem

I have a table like this: ``` num | type -----+------ 123 | 3 123 | 2 123 | 3 123 | 2 124 | 3 124 | 1 124 | 3 124 | 3 ``` I want to group by the `num` column, and have a column counting each distinct type (I know all the possible values here in advance). So I would get: ``` num | 1 | 2 | 3 -----+---+---+--- 123 | 0 | 2 | 2 124 | 1 | 0 | 3 ``` Is this possible with SQL? I am using MySql.

Original source