sql - Grouping result in show in columnar format in MySQL -


i stuck somewhere count results , converting them columns. tried pivoting, not it. can please on following:

e   w   101 p   1 102 p   3 101 q   4 102 q   4 103 p   3 103 q   1 104 p   5 104 q   1 105 p   3 105 q   2 

and output follows:

w   1   2   3   4   5 p   1   0   3   0   1 q   2   1   0   2   0 

the logic wanted count no of time 1,2,3,4,5 appeared each w. example, w=p , a=3 appeared thrice, w=p , a=1 appeared once.

as barmer's answer , prefer use case when this:

select w,      sum(case when = 1 1 else 0 end) `1`,      sum(case when = 2 1 else 0 end) `2`,      sum(case when = 3 1 else 0 end) `3`,      sum(case when = 4 1 else 0 end) `4`,     sum(case when = 5 1 else 0 end) `5` yourtable group w 

Comments

Popular posts from this blog

android - MPAndroidChart - How to add Annotations or images to the chart -

javascript - Add class to another page attribute using URL id - Jquery -

firefox - Where is 'webgl.osmesalib' parameter? -