sql - 编写单个 SQL 查询以计算具有“真”条件的多个布尔列的计数
问题描述
如何在一个 SQL 查询中写入多个计数。
例如,我想要一个查询中的所有布尔功能计数器,而不是下面的所有查询。
select count(feature1) from my_table where feature1 = True
select count(feature2) from my_table where feature2 = True
select count(feature3) from my_table where feature3 = True
解决方案
使用如下条件聚合
select
sum(case when feature1= 'True' then 1 else 0 end),
sum(case when feature2= 'True' then 1 else 0 end),
sum(case when feature3= 'True' then 1 else 0 end)
from my_table
推荐阅读
- c++ - 在 MacOS 上写入串行端口时出现问题
- reflection - Kotlin 和使用 kotlin-reflect 对属性进行反射
- batch-file - 检查文件名是否包含子字符串
- javascript - 格式化文本使条件介于 2 个单词之间
- algorithm - 找到具有特定总和的数字对
- c++ - 位设置为浮点值或双精度值 C++
- javascript - 用于浏览网站的 JavaScript 脚本?
- sqlite - System.BadImageFormatException:试图加载格式不正确的程序。(来自 HRESULT 的异常:0x8007000B)在
- swift - 在 Swift 泛型类型中包含继承约束
- php - Laravel 项目 - 邮递员转义字符