开发者

How to get count in this query?

开发者 https://www.devze.com 2023-03-16 15:41 出处:网络
I have one table with following columns: ListID----Status 开发者_如何学Go1---------YES 2---------YES

I have one table with following columns:

ListID----           Status

开发者_如何学Go1---------             YES

2---------             YES

3----------             NO 

I want to write an SQL statement which will return count for each ListID column having Status as "YES".

How do I create this SQL statement, so that I get ListID 3 in it with Count as 0?

Please let me know.


Use MySQL IF-function:

If the Status is YES, count the field. Else don't count it (0).

SELECT ListID, SUM(IF(Status="YES",1,0)) 
FROM yourtable
GROUP BY `ListID`

Checkout this previous answer: Get paid cash amount grouped by day


TRY THIS

SELECT ListID, SUM(decode(Status,'YES',1,0))  
FROM yourtable 
GROUP BY ListID


You can use this very trick:

say YES is 1 and NO is 0 then just SUM(status) group by listId.


For this is enough SQL92 statement:

SELECT ListId, COUNT(*)
  FROM table
 GROUP BY ListId, Status
HAVING Status = 'Yes'


select status, count(ListID) 
from tablename
group by status
0

精彩评论

暂无评论...
验证码 换一张
取 消