46

I need some help with a T-SQL query. I want to count fields that have a special value(e.g. >1).

Assuming i have a table like:

IGrp | Item | Value1 | Value2
#############################
A    | I11  | 0.52   | 1.18
A    | I12  | 1.30   | 0.54
A    | I21  | 0.49   | 2.37
B    | I22  | 2.16   | 1.12
B    | I31  | 1.50   | 0.28

I want a result like:

IGrp | V1High | V2High 
######################
A    | 1      | 2
B    | 2      | 1

In my mind this should be going with this expression

SELECT IGrp, COUNT(Value1>1) AS V1High, COUNT(Value2>1) AS V2High
FROM Tbl GROUP BY IGrp

But that's not possible in T-SQL since the Count() does not take boolean values. So is it really the only possible way to do multiple queries with WHERE Value>1 and COUNT(*) and join them afterwards? Or is there a trick to accomplish the desired result?

Thanks in advance.

6 Answers 6

75
SELECT IGrp, 
    COUNT(CASE WHEN Value1 > 1 THEN 1 ELSE NULL END) AS V1High, 
    COUNT(CASE WHEN Value2 > 1 THEN 1 ELSE NULL END) AS V2High 
FROM Tbl
GROUP BY IGrp
Sign up to request clarification or add additional context in comments.

1 Comment

Based on my experience SQL Server can work well with this command.
12

You can use the CASE statement:

SELECT IGrp, 
    SUM(CASE WHEN Value1>1 THEN 1 ELSE 0 END) AS V1High, 
    SUM(CASE WHEN Value2>1 THEN 1 ELSE 0 END) AS V2High 
FROM Tbl GROUP BY IGrp 

2 Comments

This won't work. All non-null values are included in the count.
@HoseynHeydari no, check the edits, I corrected and the example is now correct. If an answer is wrong on SO, it is deleted.
3

make use of case when will do work for you

SELECT IGrp, 
 sum(case when isnull(Value1,0)>1 then 1 else 0 end) AS V1High, 
 sum(case when isnull(Value2,0)>1 then 1 else 0 end) AS V2High 
FROM Tbl GROUP BY IGrp 

3 Comments

Thanks, somehow i didn't thought of this.
This won't work. All non-null values are included in the count.
That's not what I meant. Your CASE statement returns either 1 or 0, meaning that the COUNT is incremented in either case. Your CASE statement should return NULL when you don't want the count to be incremented.
2
SELECT IGrp, 
    COUNT(CASE WHEN Value1 = 'Foo' THEN 1 ELSE NULL END) AS Tot_Foo, 
    COUNT(CASE WHEN Value1 = 'Blah' THEN 1 ELSE NULL END) AS Tot_Blah 
FROM Tbl
GROUP BY IGrp

This can also be used to compare 2 different values for the same field, with minor changes as shown above.

Very helpful for verifying values that are supposed to exist in a 1:1 ratio.

Comments

0

You can put a CASE .. WHEN .. statement inside the COUNT() functions to return 1 when the conditions hold, NULL otherwise.

Comments

0

You can also use:

select
count(nullif(field > minvalue,false))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.