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
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;
tsp has n number of names, above query works well for fixed tspTry 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;