0

I have a MySQL table with 20 fields. out of 20, 15 have 3 possible value like 0,NA,1+. i need to write a query to fetch each field count which has value > 0. is it possible to get it in single query.?

Thanks Guys.

2
  • tizag.com/mysqlTutorial/mysqlcount.php Commented Nov 29, 2011 at 18:11
  • @danontheline.. It's just record count. I asked about field-wise count. Commented Nov 29, 2011 at 18:15

2 Answers 2

1

Unless I'm missing something...

SELECT
    COUNT(CASE WHEN c1<>'0' THEN 1 END) AS c1_count,
    COUNT(CASE WHEN c2<>'0' THEN 1 END) AS c2_count,
    -- ...
    COUNT(CASE WHEN c15<>'0' THEN 1 END) AS c15_count
FROM t
Sign up to request clarification or add additional context in comments.

Comments

0
select "column1", count(*) from your_table where value>0
union all
select "column2", count(*) from your_table where value>0
...
... // repeat until column20

interesting documentation :- http://dev.mysql.com/doc/refman/5.0/en/union.html

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.