1

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

enter image description here

Output

enter image description here

2 Answers 2

1

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;
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks @yogesh-sharma, Assume that tsp has n number of names, above query works well for fixed tsp
1

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;

1 Comment

Assume that tsp has n number of names, above query works well for fixed tsp

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.