Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Count

Author  Topic 

cutiebo2t
Constraint Violating Yak Guru

256 Posts

Posted - 2008-05-27 : 06:47:50
I have 1 column that composes of 15 Yes and 13 No. What's the formula in getting the count of each (Yes and No)?

Thanks

harsh_athalye
Master Smack Fu Yak Hacker

5581 Posts

Posted - 2008-05-27 : 06:55:38
[code]Select
Sum(Case when col = 'Yes' then 1 else 0 end) as [Yes_Count],
Sum(Case when col = 'No' then 1 else 0 end) as [No_Count]
From Table[/code]

Harsh Athalye
India.
"The IMPOSSIBLE is often UNTRIED"
Go to Top of Page

cutiebo2t
Constraint Violating Yak Guru

256 Posts

Posted - 2008-05-27 : 06:58:04
great thanks
Go to Top of Page

cutiebo2t
Constraint Violating Yak Guru

256 Posts

Posted - 2008-05-27 : 07:07:59
why am getting a result of 0? Is there something wrong with my statement?

SELECT SUM(CASE WHEN datereviewed = 'Yes' THEN 1 ELSE 0 END) / COUNT(datereviewed)
FROM dbo.test1
Go to Top of Page

harsh_athalye
Master Smack Fu Yak Hacker

5581 Posts

Posted - 2008-05-27 : 07:11:30
quote:
Originally posted by cutiebo2t

why am getting a result of 0? Is there something wrong with my statement?

SELECT SUM(CASE WHEN datereviewed = 'Yes' THEN 1 ELSE 0 END) * 1.0 / COUNT(datereviewed)
FROM dbo.test1



Harsh Athalye
India.
"The IMPOSSIBLE is often UNTRIED"
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2008-05-27 : 07:38:16
or

SELECT SUM(CASE WHEN datereviewed = 'Yes' THEN 1.0 ELSE 0.0 END) / COUNT(datereviewed)
FROM dbo.test1


E 12°55'05.25"
N 56°04'39.16"
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2008-05-27 : 10:23:41
quote:
Originally posted by cutiebo2t

why am getting a result of 0? Is there something wrong with my statement?

SELECT SUM(CASE WHEN datereviewed = 'Yes' THEN 1 ELSE 0 END) / COUNT(datereviewed)
FROM dbo.test1


http://sqlblogcasts.com/blogs/madhivanan/archive/2008/01/16/beware-of-implicit-conversions.aspx

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -