按多种条件计算

Pou*_*rif 0 sql t-sql sql-server

这是我的数据,我需要以最快的方式获得结果.

在此输入图像描述

Gio*_*uri 5

标准条件聚合:

select city,
       sum(case when active = 'true' then 1 else 0 end) active,
       sum(case when blacklist = 'true' then 1 else 0 end) blacklist,
       sum(case when license = 'true' then 1 else 0 end) license,
       sum(case when married = 'true' then 1 else 0 end) married
from TableName
group by city
Run Code Online (Sandbox Code Playgroud)