0
    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

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Well I wanna group with the value inside codeX, so not only for something. I want to have result for all values in codeX
actually, I only need count of appearance, any ideas?
@tugce . . . Just use count(*).
0
SELECT productCode, count(*) AS count
FROM
(
    SELECT Code1 AS productCode FROM table
    UNION ALL
    SELECT Code2 FROM table
     UNION ALL
    SELECT Code3 FROM table
     UNION ALL
    SELECT Code4 FROM table
     UNION ALL
    SELECT Code5 FROM table

) AS codes
GROUP BY productCode
ORDER BY count DESC

Comments

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.