Reputation: 45
I need a query to Count distinct results...
My Table
ID | stats | name
-----------------
1 | 1 | John
2 | 1 | John
3 | 2 | John
4 | 2 | John
5 | 3 | John
i need query like this....
SELECT if( stats = 2, ADD + 1, 0) as ok, if(stats = 3, ADD + 1, 0) as no_ok
Thanks.
Upvotes: 1
Views: 328
Reputation: 56779
If you want to simultaneously count the number of rows with multiple specific criteria in a data set, you can use the pattern COUNT(CASE WHEN criteria THEN 1 END)
. Here's an example that counts the number of rows for stats = 2
, and for stats = 3
:
SELECT
count(case when stats = 2 then 1 end) as ok,
count(case when stats = 3 then 1 end) as not_ok
from
Table1
Results:
OK | NOT_OK
-----------
2 | 1
Demo: http://www.sqlfiddle.com/#!2/82414/1
Upvotes: 4