Count record between multiple columns

0

Good morning / Good afternoon. My question is the following. I have a table that has 12 columns that are all of different actions, but that each one of those actions can be qualified as EE, EC, OM. What I need is to be able to count how many EE there are between those twelve columns. As an Excel account. Is there a possibility to do it without doing a count column by column? I found a way to do it but by procedure that unfortunate does not help me since the resulting query I have to insert it into a variable of a highchart to generate a graph. I appreciate the help. Thank you very much.

    
asked by Juan 09.03.2017 в 22:58
source

1 answer

2

In this case, a SUM with IF is more useful, which is similar to COUNAR.SI:

SELECT field1, field2, ( IF(col01='EE',1,0) + IF(col02='EE',1,0) + .... ) as sum_cnt_ee  
FROM table1

This only if the account is for each row (how many EE are in each row) If you want to count them in a group of grouped rows, you would have to apply SUM to the previous result:

SELECT field1, field2, SUM( IF(col01='EE',1,0) + IF(col02='EE',1,0) + .... ) as cnt_ee  
FROM table1 
GROUP BY field1, field2

If you only want to obtain the total of EE's there would be no need to group:

SELECT SUM( IF(col01='EE',1,0) + IF(col02='EE',1,0) + .... ) as cnt_total_ee  
FROM table1 
WHERE ....
    
answered by 09.03.2017 / 23:06
source