简体   繁体   中英

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.

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.

You can use conditional aggregation to do this. Conditions in sum return 1 or 0 depending on true or false.

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.

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

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 ;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM