18

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!

4

2 回答 2

42

好吧,如果您可以过滤整个查询,那么 LittleBobbyTables 已经为您提供了答案。如果没有,您可以像这样获得该列:

count(distinct case when attribute1 > 0 then id end) -- implicit null-else, iirc
于 2012-07-26T19:05:40.837 回答
4

你几乎拥有它:

SELECT COUNT(DISTINCT [ID]) AS DistinctID
FROM YourTable
WHERE attribute1 > 0
于 2012-07-26T19:00:30.163 回答