Help with T-SQL query

Hi guys,

I have got a business situation and want to calculate the values depending on different scenarios. Can you please help me with this.  for simplification i am including the following  SQL query and not the actual lengthy query
[code][/declare @table table (id int, statusr varchar(20), reason varchar(100))
insert into @table  values (1,'a' ,'N') , (2,'b','N'), (3,'c' ,'N') ,(4,'d','D')

select COUNT(case when statusr IN ('a','b','c') then 1 else 0 end) as test1
,COUNT(case when statusr IN ('b','c') then 1 else 0 end) as test2 from @table where reason = 'N'code]

I am expecting test1 to return 3 and test2 to return 2 but both of them return same result. Where am i doing anything wrong?

Regards
Ali ShahSQL DeveloperAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jim HornMicrosoft SQL Server Data DudeCommented:
>select COUNT(case when statusr IN ('a','b','c') then 1 else 0 end)
COUNT will count all 0's and 1's as 1.  Guessing you intend to use SUM, where the 1's are added and the 0's are esstially ignored.

declare @table table (id int, statusr varchar(20), reason varchar(100))

insert into @table 
VALUES (1,'a' ,'N') , (2,'b','N'), (3,'c' ,'N') ,(4,'d','D')

select 
	SUM(case when statusr IN ('a','b','c') then 1 else 0 end) as test1,
	SUM(case when statusr IN ('b','c') then 1 else 0 end) as test2 
FROM @table 
WHERE reason = 'N'

Open in new window


This and other CASE block fun can be found in the article SQL Server CASE Solutions.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Éric MoreauSenior .Net ConsultantCommented:
replace COUNT with SUM and it will work
PortletPaulEE Topic AdvisorCommented:
COUNT() ignores NULLs but it will increment the count for ALL non-null values (including 0)

You can continue to use COUNT() by omitting the "else condition"
SELECT
  COUNT(CASE WHEN statusr IN ('a', 'b', 'c') THEN 1 END) AS test1,
  COUNT(CASE WHEN statusr IN ('b', 'c') THEN 1 END) AS test2
FROM @table
WHERE reason = 'N'

Open in new window

OR, include the "else condition" but return NULL instead of zero

SELECT
  COUNT(CASE WHEN statusr IN ('a', 'b', 'c') THEN 1 else null END) AS test1,
  COUNT(CASE WHEN statusr IN ('b', 'c') THEN 1 else null END) AS test2
FROM @table
WHERE reason = 'N'

Open in new window

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
SQL

From novice to tech pro — start learning today.