Skip to content
Advertisement

SQL query to count number of accounts based on criteria A only, B only and both A&B

Basically, I have a table that tells about type of communication to accounts(either by letter or email) like :

ACOUNT_ID TYPE_OF_COMM
1 Letter
1 Letter
3 EMail
2 Letter
4 EMail
4 Letter

and so on.. A query needs to be written that counts :

  1. the number of accounts that received letter only,
  2. the number of accounts that received mails only,
  3. the number of accounts that received both letters and mails

So the output of above should be :

Type_of_comms Count of Accounts
Letters only 2
EMail only 1
both letters and mails 1

Thanks in advance!

Advertisement

Answer

I’m not familiar with Athena, but as long as it supports CTEs / subqueries, this should work:

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement