Reputation: 16081
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
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
Reputation: 32690
You pretty much had it:
SELECT COUNT(DISTINCT [ID]) AS DistinctID
FROM YourTable
WHERE attribute1 > 0
Upvotes: 6