0

I'm looking to run a count SQL query on multiple columns at once. 83 of them. For each, I'd simply like to figure out how many instances there are in which the value = 1.

ex.

select count(*) from [filename.filename] where [Column001] = 1

select count(*) from [filename.filename] where [Column002] = 1

All data in each column is marked with wither a 0 or a 1.

Instead of writing 83 small queries, is there a way for me to write it all in one query and have them all display as a table with the results?

1
  • show a sample of data and the expected result please Commented Sep 14, 2016 at 18:21

1 Answer 1

3

This seems to be what you want:

SELECT SUM(CASE WHEN Column_1 = 1 THEN 1 ELSE 0 END) N_1,
       SUM(CASE WHEN Column_2 = 1 THEN 1 ELSE 0 END) N_2,
       SUM(CASE WHEN Column_3 = 1 THEN 1 ELSE 0 END) N_3,
       .....
       SUM(CASE WHEN Column_83 = 1 THEN 1 ELSE 0 END) N_83
FROM YourTable;
Sign up to request clarification or add additional context in comments.

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.