table1
person | zipcode | timestamp | event | device
--------------------------------------------------|--------|------
amar | 11111 | 2016-09-28 20:05:03.001 | email | phone
akbar | 11111 | 2016-09-28 20:05:03.001 | email | phone
antony | 11111 | 2016-09-28 20:07:03.001 | chat | pc
amar | 11111 | 2016-09-28 20:08:03.001 | email | phone
amar | 11111 | 2016-09-28 20:08:03.001 | chat | phone
amar | 22222 | 2016-09-28 20:09:03.001 | email | phone
akbar | 22222 | 2016-09-28 20:10:03.001 | email | phone
antony | 22222 | 2016-09-28 20:10:03.001 | chat | phone
amar | 11111 | 2016-09-28 21:05:03.001 | email | phone
akbar | 11111 | 2016-09-28 21:05:03.001 | email | phone
antony | 11111 | 2016-09-28 21:07:03.001 | chat | phone
output desired
person | total_events | email_events
---------------------------------------
amar | 5 | 4
akbar | 3 | 3
antony | 3 | 0
Thinking of using group by for this like below - how to get count of events where event = 'email' here - is there a way to do this without using a sub-select?
select
person
, count (*) as total_events
from table1
group by person
... count(*) filter (where event = 'email') as email_events