有没有办法在t-sql中为count指定where子句?
我想做的事(请注意这是一个好的编码)是:
SELECT COUNT([column] WHERE [column]>20) AS [a],
COUNT([column] WHERE [column]<20 AND [column]>10) AS [b]
FROM [table];
而不是两次查询
SELECT COUNT(*) FROM [table] WHERE [column]>20;
SELECT COUNT(*) FROM [table] WHERE [column]<20 AND [column]>10;
答案 0 :(得分:2)
使用简单的案例陈述:
select
count (case when [column]>20 then 1 else null end) as [a],
count (case when [column]<20 and [column]>10 then 1 else null end) as [b]
from
table