14

I recently set up a MYSQL database connected to a form filled with checkboxes. If the checkbox was selected, it would insert into the associated column a value of '1'; otherwise, it would receive a value of '0'.

I'd like to eventually look at aggregate data from this form, and was wondering if there was any way I could use MYSQL to get a number for each column which would be equal to the number of rows that had a value of '1'.

I've tried variations of:

select count(*) from POLLDATA group by column_name

which was unsuccessful, and nothing else I can think of seems to make sense (admittedly, I'm not all too experienced in SQL).

I'd really like to avoid:

select count(*) from POLLDATA where column_1='1'

for each column (there close to 100 of them). Is there any way to do this besides typing out a select count(*) statement for each column?

EDIT:

If it helps, the columns are 'artist1', 'artist2', ....'artist88', 'gender', 'age', 'city', 'state'. As I tried to explain below, I was hoping that I'd be able to do something like:

select sum(EACH_COLUMN) from POLLDATA where gender='Male', city='New York City';

(obviously EACH_COLUMN is bogus)

6
  • 2
    Show some sample data to be easier to help you Commented Nov 11, 2013 at 18:14
  • You want to do your counts based on ~100 different columns?
    – Andrew
    Commented Nov 11, 2013 at 18:16
  • Hey, you changed the question after I answered below. Commented Nov 11, 2013 at 18:17
  • You have 100 columns? It sounds like there's something wrong with your design.
    – Strawberry
    Commented Nov 11, 2013 at 18:18
  • @Andrew, the form has a listing of a bunch of musical artists and is associated with some information about each user. My idea for the implementation was to have a column for each artists, so that when the form closed I could filter searches by all the other information (e.g. by using where gender='male'). I was looking to be able to easily compare the counts for all of the columns associated with the artists to see how they did in aggregate with a specific set of filters on my selection.
    – dryan93
    Commented Nov 11, 2013 at 18:23

6 Answers 6

40
SELECT SUM(CASE 
             WHEN t.your_column = '1' THEN 1
             ELSE 0
           END) AS OneCount,
       SUM(CASE 
             WHEN t.your_column='0' THEN 1
             ELSE 0
           END) AS ZeroCount
  FROM YOUR_TABLE t 
4
  • That's an extremely verbose way to write SUM(your_column). And he never asked for a count of rows with zero.
    – Barmar
    Commented Nov 11, 2013 at 18:24
  • That's true, must have misunderstood. And it is verbose I know, seemed like a one off thing for OP, so went with it.
    – Vishal
    Commented Nov 11, 2013 at 18:27
  • 5
    While it may not be exactly what the OP was looking for - it DID provide the answer for what I was looking for. Thanks, Vishal!
    – Beto
    Commented Jun 14, 2016 at 17:50
  • 3
    This is a good way to sum on specific values in columns, such as sum( case when order_status = 4 then 1 else 0 end) as completed sum( case when order_status = 50 then 1 else 0 end) as cancelled Commented Jun 2, 2017 at 19:11
7

If you are just looking for the sheer number of 1's in the columns, you could try…

select sum(col1), sum(col2), sum(col3) from POLLDATA
2
  • Sorry! I was trying to add some more information to be more helpful.
    – dryan93
    Commented Nov 11, 2013 at 18:18
  • That would definitely work, thanks. I was hoping to avoid having to type that out for all 100 columns, but its entirely possible this is the best solution
    – dryan93
    Commented Nov 11, 2013 at 18:33
6

A slightly more compact notation than SUM ( CASE WHEN ... ELSE END) is SUM( IF( expression ) ).

For the askers example, this could look something like:

select
  count(*) as total,
  sum(if(gender = 'MALE', 1, 0)) as males,
  sum(if(gender = 'FEMALE', 1, 0)) as females,
  sum(if(city = 'New York City', 1, 0)) as newYorkResidents
from POLLDATA;

Example result:

+-------+-------+---------+------------------+
| total | males | females | newYorkResidents |
+-------+-------+---------+------------------+
|    42 |    23 |      19 |               42 |
+-------+-------+---------+------------------+
1

select count(*) from POLLDATA group by column_name

I dont think you want to do a count cause this will also count the records with a 0.

try

 select column_naam,sum(column_name) from POLLDATA group by column_name

or

select column_naam,count(*) from POLLDATA 
 where column_name <> 0 
 group by column_name

only adds the 0

0

Instead of strings why not store actual numbers, 1 or 0.

Then you could use the sql SUM function.

1
  • The data types are actually ints, if that's what you mean (quotes worked interchangeably in my selection queries so I went ahead and added them to my description, sorry for the confusion).
    – dryan93
    Commented Nov 11, 2013 at 18:30
0

When the query begins to be a little too complicated, maybe it's because you should think again about your database structure. But if you want to keep your table as it is, you could use a prepared statement that automatically calculates all the sums for you, without specifying every single column:

SELECT
  CONCAT(
    'SELECT ',
    GROUP_CONCAT(CONCAT('SUM(', `column_name`, ') AS sum_', `column_name`)),
    ' FROM POLLDATA WHERE gender=? AND city=?')
FROM   `information_schema`.`columns` 
WHERE  `table_schema`=DATABASE() 
       AND `table_name`='POLLDATA'
       AND `column_name` LIKE 'artist%'
INTO @sql;

SET @gender := 'male';
SET @city := 'New York';

PREPARE stmt FROM @sql;
EXECUTE stmt USING @gender, @city;

Please see fiddle here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.