Mo.Ashfaq Mo.Ashfaq - 7 months ago 17
SQL Question

How to count for a specific value column wise?

I have made the table with 4 columns using mysql.

Each column have datatype as

BIT
.

I have data like this:

ColA ColB ColC Cold
1 1 1 1
1 0 0 1
0 0 1 0


I wants to have query that will return the column name and no.of 1s it will have, like following:

ColA ColB ColC Cold
2 1 2 2


I have thinked about group by, aggregate function, but not able to design the query.

Any help would be appreciated

Answer

From your comment to AK47's answer, you may prefer

select 
sum(colA) as colA, 
sum(colB) as colB, 
sum(colC) as colC, 
sum(colD) as cold -- I'm frozen
from Table1

See the diff in sqlFiddle

Comments