Let's say I have the following table:
Id color
A00 blue
A00 blue
A99 red
A99 blue
A95 yellow
A97 green
I would like to get something like:
Id blue red yellow green
A00 2 0 0 0
A99 1 1 0 0
A95 0 0 1 0
A97 0 0 0 1
What's the easiest way of doing this?
I thought about this:
select Id,
sum(case when color='blue' then 1 else 0 end) as blue,
sum(case when color='red' then 1 else 0 end) as red,
.
.
.
from table
The problem is that I have so many colours that doing this would be exhausting. Is there an easier way?