Advertisement

05.21.2008 at 04:11AM PDT, ID: 23420295
[x]
Attachment Details

How to use a SQL CASE statement to count rows with a 0 value

Asked by ihatelag in MS SQL Server, SQL Server 2005

Tags: SQL

Hi there,

I am trying to count some rows using a case statement, but the value is 0. how do I get to count each row so I know how many rows contain the value 0?

I tried Count(CASE when .... but the value seemed to be wrong and very large.

Many thanks for your help!Start Free Trial
1:
2:
3:
4:
5:
6:
7:
8:
SELECT     sum(CASE when S.Compl between 1 and 20 AND S.risk = 1 then S.risk else 0 end) AS AMTGain, -- it's good perfect
           sum(CASE when S.Compl between 1 and 20 AND S.risk = 0 then S.risk else 0 end) AS AMTLoser,
        
FROM       tbl_Transact AS S with (nolock)
           join tbl_Custs as u with (nolock) on s.idutente = u.idutente 
 
WHERE (u.testuser = 0)  -- no test user
AND (CONVERT(CHAR(8), S.Date, 112) BETWEEN '20080520' AND '20080520')
[+][-]05.21.2008 at 04:13AM PDT, ID: 21613744

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zones: MS SQL Server, SQL Server 2005
Tags: SQL
Sign Up Now!
Solution Provided By: TimCottee
Participating Experts: 1
Solution Grade: A
 
 
 
Loading Advertisement...
20080716-EE-VQP-32 / EE_QW_2_20070628