Cod*_*lus 15 t-sql conditional count distinct
如何实现以下psuedo-SQL语句的"含义":
COUNT(distinct id where attribute1 > 0)
Run Code Online (Sandbox Code Playgroud)
换句话说,我如何制作有条件的,不同的计数陈述?
谢谢!
I a*_*ica 38
好吧,如果您可以过滤整个查询,那么LittleBobbyTables已经为您提供了答案.如果没有,您可以像这样获得该列:
count(distinct case when attribute1 > 0 then id end) -- implicit null-else, iirc
Run Code Online (Sandbox Code Playgroud)
你几乎已经拥有了:
SELECT COUNT(DISTINCT [ID]) AS DistinctID
FROM YourTable
WHERE attribute1 > 0
Run Code Online (Sandbox Code Playgroud)