[英]SQL - Filter on two columns
I am trying to get of the count of two columns with a where clause on each column individually in sql. 我正在尝试在sql中每列上分别带有where子句的两列的计数。
Lets say my data looks like 可以说我的数据看起来像
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
Now, I want to group the data by person and the grouped data should look like 现在,我想按人对数据进行分组,分组后的数据应类似于
person feature1 feature2
a 2 1
b 0 1
c 1 0
I wanted to count the no of zeros of each column per person. 我想计算每人每列的零数。 How can I do this through sql.
我如何通过sql做到这一点。
You can use conditional aggregation to do this. 您可以使用条件聚合来执行此操作。 Conditions in
sum
return 1 or 0 depending on true or false. sum
条件返回1或0(取决于是或否)。
select person,sum(feature1=0),sum(feature2=0)
from tbl
group by person
In Hive, you should cast the boolean returned to int
before summing up. 在Hive中,您应该在总结之前将返回的布尔值转换为
int
。
select person,sum(cast(feature1=0 as int)),sum(cast(feature2=0 as int))
from tbl
group by person
Here you can use case statement to count non zero features for each person 在这里,您可以使用case语句为每个人计算非零特征
select person, count(case when feature1>0 then 1 else null end) F1, count(case when feature1>0 then 1 else null end) F2 from Table1 group by person ; 从表1中按人员分组选择人员,计数(在feature1> 0时为1,否则为1的另一端)的情况下F1,计数(在feature1> 0时,则为1,否则为0的另一端情况)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.