CodeKingPlusPlus
CodeKingPlusPlus

Reputation: 16081

TSQL Count 'Where' Condition

How do I implement the 'meaning' of the following psuedo-SQL statement:

COUNT(distinct id where attribute1 > 0)

In other words, how do I make conditional, distinct counting statements?

Thanks!

Upvotes: 21

Views: 27381

Answers (2)

canon
canon

Reputation: 41675

Well, if you can filter the entire query, then LittleBobbyTables already has the answer for you. If not, you can get that column like so:

count(distinct case when attribute1 > 0 then id end) -- implicit else null

Upvotes: 48

You pretty much had it:

SELECT COUNT(DISTINCT [ID]) AS DistinctID
FROM YourTable
WHERE attribute1 > 0

Upvotes: 6

Related Questions