Reputation: 57
I have a main table as first image and I need to produce output as second table. Can any one please help me to do mysql query.
Main Table
Output
Upvotes: 1
Views: 63
Reputation: 13506
Try with CASE WHEN
select anumber,
sum(case when tsp = 'aplace' then 1 else 0 end) as aplace,
sum(case when tsp = 'bplace' then 1 else 0 end) as bplace,
sum(case when tsp = 'cplace' then 1 else 0 end) as cplace,
from table t
group by anumber;
Upvotes: 1
Reputation: 50173
You can do conditional aggregation :
select anumber,
sum(tsp = 'aplace') as aplace,
sum(tsp = 'bplace') as bplace,
sum(tsp = 'cplace') as cplace
from table t
group by anumber;
Upvotes: 1