Reputation: 21518
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.
Upvotes: 4
Views: 890
Reputation: 204784
SELECT `num`,
SUM(type = 1) as `1`,
SUM(type = 2) as `2`,
SUM(type = 3) as `3`
FROM `your_table`
GROUP BY `num`
Upvotes: 7