code1 | code2 | code3 | code4 | code5
row1 A B C
row2 B A D E
row3 F C
How can I group this table with count of value in codeX column So
A: 2
B: 2
C: 2
D: 1
E: 1
F: 1
If I understand correctly, you want a list of the columns where something appears. You can unpivot the table an use group_concat:
select code, count(*) as cnt, group_concat(which)
from (select code1 as code, 'code1' as which from table union all
select code2, 'code2' as which from table union all
select code3, 'code3' as which from table union all
select code4, 'code4' as which from table
) c
where code is not null
group by code;
I used the full column names instead of #1 -- it seems more useful. But you can put in #1 etc for which.
count(*).