【问题标题】:Where clause in count计数中的 Where 子句
【发布时间】:2014-07-14 15:34:33
【问题描述】:

有没有什么方法可以在 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;

【问题讨论】:

标签: sql sql-server tsql count where-clause


【解决方案1】:

使用简单的 case 语句:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-10
    • 1970-01-01
    • 1970-01-01
    • 2021-03-28
    • 1970-01-01
    • 1970-01-01
    • 2014-10-12
    • 1970-01-01
    相关资源
    最近更新 更多