如果您不能仅使用where子句限制查询本身,则可以使用以下事实:count聚合仅计算非空值:select count(case Position when 'Manager' then 1 else null end)from ...您也可以sum类似的方式使用聚合:select sum(case Position when 'Manager' then 1 else 0 end)from ...
假设您也不想因为聚合其他值而限制返回的行,则可以这样做:select count(case when Position = 'Manager' then 1 else null end) as ManagerCountfrom ...假设在同一列中您具有经理,主管和团队负责人的值,您可以像这样获得每个值:select count(case when Position = 'Manager' then 1 else null end) as ManagerCount, count(case when Position = 'Supervisor' then 1 else null end) as SupervisorCount, count(case when Position = 'Team Lead' then 1 else null end) as TeamLeadCount,from ...
取决于您的意思,但含义的另一种解释是您要对具有特定值的行进行计数,但又不想将仅限SELECT于这些行...您可以使用SUM()带有这样的子句的方法来代替,COUNT()例如:SELECT SUM(CASE WHEN Position = 'Manager' THEN 1 ELSE 0 END) AS ManagerCount, SUM(CASE WHEN Position = 'CEO' THEN 1 ELSE 0 END) AS CEOCountFROM SomeTable