Mysql SQL-两列上的筛选器
我试图在sql中分别使用where子句获取两列的计数 假设我的数据看起来像Mysql SQL-两列上的筛选器,mysql,sql,hiveql,Mysql,Sql,Hiveql,我试图在sql中分别使用where子句获取两列的计数 假设我的数据看起来像 person feature1 feature2 a 1 1 a 0 1 a 1 1 a 1 1 a 0 0 a 1 1 b 0 1 c 1 0 现在,我想按个人
person feature1 feature2
a 1 1
a 0 1
a 1 1
a 1 1
a 0 0
a 1 1
b 0 1
c 1 0
现在,我想按个人对数据进行分组,分组后的数据应该如下所示
person feature1 feature2
a 2 1
b 0 1
c 1 0
我想计算每个人每列的零的数量。如何通过sql实现这一点。您可以使用条件聚合来实现这一点。
sum
中的条件根据true或false返回1或0
select person,sum(feature1=0),sum(feature2=0)
from tbl
group by person
在配置单元中,应该在求和之前将返回的布尔值强制转换为int
select person,sum(cast(feature1=0 as int)),sum(cast(feature2=0 as int))
from tbl
group by person
您可以使用条件聚合来实现这一点。
sum
中的条件根据true或false返回1或0
select person,sum(feature1=0),sum(feature2=0)
from tbl
group by person
在配置单元中,应该在求和之前将返回的布尔值强制转换为int
select person,sum(cast(feature1=0 as int)),sum(cast(feature2=0 as int))
from tbl
group by person
在这里,您可以使用case语句统计每个人的非零特征 选人,, 计数(当feature1>0时,则为1,否则为空结束)F1, 计数(当feature1>0时,则为1,否则为空结束)F2 来自表1
分组 在这里,您可以使用case语句统计每个人的非零特征 选人,, 计数(当feature1>0时,则为1,否则为空结束)F1, 计数(当feature1>0时,则为1,否则为空结束)F2 来自表1
分组 b行的功能1应为1 i猜测b行的功能1应为1 i猜测
else null
是冗余的else null
是冗余的